Skip to content

BUG-17280 to_html follows display.precision for column numbers in notebooks #25914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Apr 4, 2019
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 @@ -353,6 +353,7 @@ I/O
- 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`)
- Bug in :func:`read_csv` not properly interpreting the UTF8 encoded filenames on Windows on Python 3.6+ (:issue:`15086`)
- Improved performance in :meth:`pandas.read_stata` and :class:`pandas.io.stata.StataReader` when converting columns that have missing values (:issue:`25772`)
- Bug in :meth:`DataFrame.to_html` where header numbers would ignore display options when rounding (:issue:`17280`)
- Bug in :func:`read_hdf` not properly closing store after a ``KeyError`` is raised (:issue:`25766`)
- Bug in ``read_csv`` which would not raise ``ValueError`` if a column index in ``usecols`` was out of bounds (:issue:`25623`)

Expand Down
8 changes: 7 additions & 1 deletion pandas/io/formats/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ def row_levels(self):
# not showing (row) index
return 0

def _get_columns_formatted_values(self):
return self.columns

@property
def is_truncated(self):
return self.fmt.is_truncated
Expand Down Expand Up @@ -292,7 +295,7 @@ def _write_col_header(self, indent):
row.append(self.columns.name or '')
else:
row.append('')
row.extend(self.columns)
row.extend(self._get_columns_formatted_values())
align = self.fmt.justify

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

def _get_columns_formatted_values(self):
return self.columns.format()

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
10 changes: 10 additions & 0 deletions pandas/tests/io/formats/test_to_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,3 +633,13 @@ def test_to_html_invalid_classes_type(classes):

with pytest.raises(TypeError, match=msg):
df.to_html(classes=classes)


def test_to_html_round_column_headers():
# GH 17280
df = DataFrame([1], columns=[0.55555])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add the issue number

with pd.option_context('display.precision', 3):
html = df.to_html(notebook=False)
notebook = df.to_html(notebook=True)
assert "0.55555" in html
assert "0.556" in notebook