Skip to content

Commit 0c193c6

Browse files
simonjayhawkinsjreback
authored andcommitted
Make DataFrame.to_html output full content (#24841)
1 parent ce47205 commit 0c193c6

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ MultiIndex
209209
I/O
210210
^^^
211211

212+
- Bug in :func:`DataFrame.to_html()` where values were truncated using display options instead of outputting the full content (:issue:`17004`)
212213
- Fixed bug in missing text when using :meth:`to_clipboard` if copying utf-16 characters in Python 3 on Windows (:issue:`25040`)
213214
- 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`)
214215
- 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`)

pandas/io/formats/html.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from pandas.core.dtypes.generic import ABCMultiIndex
1414

15-
from pandas import compat
15+
from pandas import compat, option_context
1616
from pandas.core.config import get_option
1717

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

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

323+
def _get_formatted_values(self):
324+
with option_context('display.max_colwidth', 999999):
325+
fmt_values = {i: self.fmt._format_col(i)
326+
for i in range(self.ncols)}
327+
return fmt_values
328+
323329
def _write_body(self, indent):
324330
self.write('<tbody>', indent)
325-
fmt_values = {i: self.fmt._format_col(i) for i in range(self.ncols)}
331+
fmt_values = self._get_formatted_values()
326332

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

495+
def _get_formatted_values(self):
496+
return {i: self.fmt._format_col(i) for i in range(self.ncols)}
497+
489498
def write_style(self):
490499
# We use the "scoped" attribute here so that the desired
491500
# style properties for the data frame are not then applied

pandas/tests/io/formats/test_to_html.py

+23
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@
1515

1616
import pandas.io.formats.format as fmt
1717

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

1928
def expected_html(datapath, name):
2029
"""
@@ -600,3 +609,17 @@ def test_to_html_render_links(render_links, expected, datapath):
600609
result = df.to_html(render_links=render_links)
601610
expected = expected_html(datapath, expected)
602611
assert result == expected
612+
613+
614+
@pytest.mark.parametrize('method,expected', [
615+
('to_html', lambda x:lorem_ipsum),
616+
('_repr_html_', lambda x:lorem_ipsum[:x - 4] + '...') # regression case
617+
])
618+
@pytest.mark.parametrize('max_colwidth', [10, 20, 50, 100])
619+
def test_ignore_display_max_colwidth(method, expected, max_colwidth):
620+
# see gh-17004
621+
df = DataFrame([lorem_ipsum])
622+
with pd.option_context('display.max_colwidth', max_colwidth):
623+
result = getattr(df, method)()
624+
expected = expected(max_colwidth)
625+
assert expected in result

0 commit comments

Comments
 (0)