Skip to content

Commit

Permalink
Make DataFrame.to_html output full content (#24841)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonjayhawkins authored and jreback committed Mar 3, 2019
1 parent ce47205 commit 0c193c6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ MultiIndex
I/O
^^^

- Bug in :func:`DataFrame.to_html()` where values were truncated using display options instead of outputting the full content (:issue:`17004`)
- Fixed bug in missing text when using :meth:`to_clipboard` if copying utf-16 characters in Python 3 on Windows (:issue:`25040`)
- Bug in :func:`read_json` for ``orient='table'`` when it tries to infer dtypes by default, which is not applicable as dtypes are already defined in the JSON schema (:issue:`21345`)
- Bug in :func:`read_json` for ``orient='table'`` and float index, as it infers index dtype by default, which is not applicable because index dtype is already defined in the JSON schema (:issue:`25433`)
Expand Down
13 changes: 11 additions & 2 deletions pandas/io/formats/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from pandas.core.dtypes.generic import ABCMultiIndex

from pandas import compat
from pandas import compat, option_context
from pandas.core.config import get_option

from pandas.io.common import _is_url
Expand Down Expand Up @@ -320,9 +320,15 @@ def _write_header(self, indent):

self.write('</thead>', indent)

def _get_formatted_values(self):
with option_context('display.max_colwidth', 999999):
fmt_values = {i: self.fmt._format_col(i)
for i in range(self.ncols)}
return fmt_values

def _write_body(self, indent):
self.write('<tbody>', indent)
fmt_values = {i: self.fmt._format_col(i) for i in range(self.ncols)}
fmt_values = self._get_formatted_values()

# write values
if self.fmt.index and isinstance(self.frame.index, ABCMultiIndex):
Expand Down Expand Up @@ -486,6 +492,9 @@ class NotebookFormatter(HTMLFormatter):
DataFrame._repr_html_() and DataFrame.to_html(notebook=True)
"""

def _get_formatted_values(self):
return {i: self.fmt._format_col(i) for i in range(self.ncols)}

def write_style(self):
# We use the "scoped" attribute here so that the desired
# style properties for the data frame are not then applied
Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/io/formats/test_to_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@

import pandas.io.formats.format as fmt

lorem_ipsum = (
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod"
" tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim"
" veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex"
" ea commodo consequat. Duis aute irure dolor in reprehenderit in"
" voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur"
" sint occaecat cupidatat non proident, sunt in culpa qui officia"
" deserunt mollit anim id est laborum.")


def expected_html(datapath, name):
"""
Expand Down Expand Up @@ -600,3 +609,17 @@ def test_to_html_render_links(render_links, expected, datapath):
result = df.to_html(render_links=render_links)
expected = expected_html(datapath, expected)
assert result == expected


@pytest.mark.parametrize('method,expected', [
('to_html', lambda x:lorem_ipsum),
('_repr_html_', lambda x:lorem_ipsum[:x - 4] + '...') # regression case
])
@pytest.mark.parametrize('max_colwidth', [10, 20, 50, 100])
def test_ignore_display_max_colwidth(method, expected, max_colwidth):
# see gh-17004
df = DataFrame([lorem_ipsum])
with pd.option_context('display.max_colwidth', max_colwidth):
result = getattr(df, method)()
expected = expected(max_colwidth)
assert expected in result

0 comments on commit 0c193c6

Please sign in to comment.