Skip to content

Commit 013f4b4

Browse files
JustinZhengBCjreback
authored andcommitted
BUG-17280 to_html follows display.precision for column numbers in notebooks (#25914)
1 parent 59feac7 commit 013f4b4

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ I/O
354354
- Bug in :meth:`DataFrame.to_string` and :meth:`DataFrame.to_latex` that would lead to incorrect output when the ``header`` keyword is used (:issue:`16718`)
355355
- Bug in :func:`read_csv` not properly interpreting the UTF8 encoded filenames on Windows on Python 3.6+ (:issue:`15086`)
356356
- Improved performance in :meth:`pandas.read_stata` and :class:`pandas.io.stata.StataReader` when converting columns that have missing values (:issue:`25772`)
357+
- Bug in :meth:`DataFrame.to_html` where header numbers would ignore display options when rounding (:issue:`17280`)
357358
- Bug in :func:`read_hdf` not properly closing store after a ``KeyError`` is raised (:issue:`25766`)
358359
- Bug in ``read_csv`` which would not raise ``ValueError`` if a column index in ``usecols`` was out of bounds (:issue:`25623`)
359360
- Improved :meth:`pandas.read_stata` and :class:`pandas.io.stata.StataReader` to read incorrectly formatted 118 format files saved by Stata (:issue:`25960`)

pandas/io/formats/html.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ def row_levels(self):
7070
# not showing (row) index
7171
return 0
7272

73+
def _get_columns_formatted_values(self):
74+
return self.columns
75+
7376
@property
7477
def is_truncated(self):
7578
return self.fmt.is_truncated
@@ -292,7 +295,7 @@ def _write_col_header(self, indent):
292295
row.append(self.columns.name or '')
293296
else:
294297
row.append('')
295-
row.extend(self.columns)
298+
row.extend(self._get_columns_formatted_values())
296299
align = self.fmt.justify
297300

298301
if truncate_h:
@@ -494,6 +497,9 @@ class NotebookFormatter(HTMLFormatter):
494497
def _get_formatted_values(self):
495498
return {i: self.fmt._format_col(i) for i in range(self.ncols)}
496499

500+
def _get_columns_formatted_values(self):
501+
return self.columns.format()
502+
497503
def write_style(self):
498504
# We use the "scoped" attribute here so that the desired
499505
# style properties for the data frame are not then applied

pandas/tests/io/formats/test_to_html.py

+10
Original file line numberDiff line numberDiff line change
@@ -633,3 +633,13 @@ def test_to_html_invalid_classes_type(classes):
633633

634634
with pytest.raises(TypeError, match=msg):
635635
df.to_html(classes=classes)
636+
637+
638+
def test_to_html_round_column_headers():
639+
# GH 17280
640+
df = DataFrame([1], columns=[0.55555])
641+
with pd.option_context('display.precision', 3):
642+
html = df.to_html(notebook=False)
643+
notebook = df.to_html(notebook=True)
644+
assert "0.55555" in html
645+
assert "0.556" in notebook

0 commit comments

Comments
 (0)