diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 1ef05ae5f9c6b..e108742319ad6 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -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`) diff --git a/pandas/io/formats/html.py b/pandas/io/formats/html.py index eba56bd0e4d87..031891cb2f7cb 100644 --- a/pandas/io/formats/html.py +++ b/pandas/io/formats/html.py @@ -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 @@ -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: @@ -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 diff --git a/pandas/tests/io/formats/test_to_html.py b/pandas/tests/io/formats/test_to_html.py index d146e9c16e114..7b975b0688e37 100644 --- a/pandas/tests/io/formats/test_to_html.py +++ b/pandas/tests/io/formats/test_to_html.py @@ -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]) + 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