From 139def2145b83d40364235c6297e1833eab7bb05 Mon Sep 17 00:00:00 2001 From: Deepak Saldanha Date: Fri, 4 Oct 2024 01:39:46 +0530 Subject: [PATCH] BUG: fix html float display (#59930) * fix html display float/strings * add test under io, update whatsnew * fix linting * changes to fix floats only * Revert "fix linting" This reverts commit 1061442e0a1cf8f745b0863762f2aa023d388336. * test script for float format * remove nbsp implementation, keep floats * Trigger CI * implement changes post review * lint check * update test_formats.py * rfc test_format.py * update test cases --- doc/source/whatsnew/v3.0.0.rst | 1 + pandas/core/frame.py | 3 ++- pandas/tests/io/formats/test_format.py | 17 +++++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 346e2b9e7997e..a5b4560a47bc4 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -620,6 +620,7 @@ I/O ^^^ - Bug in :class:`DataFrame` and :class:`Series` ``repr`` of :py:class:`collections.abc.Mapping`` elements. (:issue:`57915`) - Bug in :meth:`.DataFrame.to_json` when ``"index"`` was a value in the :attr:`DataFrame.column` and :attr:`Index.name` was ``None``. Now, this will fail with a ``ValueError`` (:issue:`58925`) +- Bug in :meth:`DataFrame._repr_html_` which ignored the ``"display.float_format"`` option (:issue:`59876`) - Bug in :meth:`DataFrame.from_records` where ``columns`` parameter with numpy structured array was not reordering and filtering out the columns (:issue:`59717`) - Bug in :meth:`DataFrame.to_dict` raises unnecessary ``UserWarning`` when columns are not unique and ``orient='tight'``. (:issue:`58281`) - Bug in :meth:`DataFrame.to_excel` when writing empty :class:`DataFrame` with :class:`MultiIndex` on both axes (:issue:`57696`) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 4c56948a48eb2..f184aab4070d7 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1192,6 +1192,7 @@ def _repr_html_(self) -> str | None: min_rows = get_option("display.min_rows") max_cols = get_option("display.max_columns") show_dimensions = get_option("display.show_dimensions") + show_floats = get_option("display.float_format") formatter = fmt.DataFrameFormatter( self, @@ -1199,7 +1200,7 @@ def _repr_html_(self) -> str | None: col_space=None, na_rep="NaN", formatters=None, - float_format=None, + float_format=show_floats, sparsify=None, justify=None, index_names=True, diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index af7b04d66096a..82cc3a838ca68 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -368,6 +368,23 @@ def test_repr_min_rows(self): assert ".." not in repr(df) assert ".." not in df._repr_html_() + @pytest.mark.parametrize( + "data, format_option, expected_values", + [ + (12345.6789, "{:12.3f}", "12345.679"), + (None, "{:.3f}", "None"), + ("", "{:.2f}", ""), + (112345.6789, "{:6.3f}", "112345.679"), + ], + ) + def test_repr_float_formatting_html_output( + self, data, format_option, expected_values + ): + with option_context("display.float_format", format_option.format): + df = DataFrame({"A": [data]}) + html_output = df._repr_html_() + assert expected_values in html_output + def test_str_max_colwidth(self): # GH 7856 df = DataFrame(