diff --git a/doc/source/whatsnew/v1.2.4.rst b/doc/source/whatsnew/v1.2.4.rst index 9cef1307278e8..fffdf333178fc 100644 --- a/doc/source/whatsnew/v1.2.4.rst +++ b/doc/source/whatsnew/v1.2.4.rst @@ -20,7 +20,7 @@ Fixed regressions - Fixed regression in (in)equality comparison of ``pd.NaT`` with a non-datetimelike numpy array returning a scalar instead of an array (:issue:`40722`) - Fixed regression in :meth:`DataFrame.where` not returning a copy in the case of an all True condition (:issue:`39595`) - Fixed regression in :meth:`DataFrame.replace` raising ``IndexError`` when ``regex`` was a multi-key dictionary (:issue:`39338`) -- +- Fixed regression in repr of floats in an ``object`` column not respecting ``float_format`` when printed in the console or outputted through :meth:`DataFrame.to_string`, :meth:`DataFrame.to_html`, and :meth:`DataFrame.to_latex` (:issue:`40024`) .. --------------------------------------------------------------------------- diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 9b7d4d92d75d1..ba406a1ef117c 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -1275,7 +1275,9 @@ def _format_strings(self) -> list[str]: float_format = get_option("display.float_format") if float_format is None: precision = get_option("display.precision") - float_format = lambda x: f"{x: .{precision:d}f}" + float_format = lambda x: _trim_zeros_single_float( + f"{x: .{precision:d}f}" + ) else: float_format = self.float_format @@ -1331,7 +1333,7 @@ def _format(x): if not is_float_type[i] and leading_space: fmt_values.append(f" {_format(v)}") elif is_float_type[i]: - fmt_values.append(_trim_zeros_single_float(float_format(v))) + fmt_values.append(float_format(v)) else: if leading_space is False: # False specifically, so that the default is diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index 17445b2f134d3..c6155cac101e6 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -2025,6 +2025,21 @@ def test_repr_str_float_truncation(self, data, expected): result = repr(series) assert result == expected + @pytest.mark.parametrize( + "float_format,expected", + [ + ("{:,.0f}".format, "0 1,000\n1 test\ndtype: object"), + ("{:.4f}".format, "0 1000.0000\n1 test\ndtype: object"), + ], + ) + def test_repr_float_format_in_object_col(self, float_format, expected): + # GH#40024 + df = Series([1000.0, "test"]) + with option_context("display.float_format", float_format): + result = repr(df) + + assert result == expected + def test_dict_entries(self): df = DataFrame({"A": [{"a": 1, "b": 2}]}) diff --git a/pandas/tests/io/formats/test_to_html.py b/pandas/tests/io/formats/test_to_html.py index 1c89c4e392a7f..ec2f109900f3a 100644 --- a/pandas/tests/io/formats/test_to_html.py +++ b/pandas/tests/io/formats/test_to_html.py @@ -883,3 +883,29 @@ def test_to_html_na_rep_and_float_format(na_rep): """ assert result == expected + + +def test_to_html_float_format_object_col(): + # GH#40024 + df = DataFrame(data={"x": [1000.0, "test"]}) + result = df.to_html(float_format=lambda x: f"{x:,.0f}") + expected = """ + + + + + + + + + + + + + + + + +
x
01,000
1test
""" + + assert result == expected diff --git a/pandas/tests/io/formats/test_to_latex.py b/pandas/tests/io/formats/test_to_latex.py index 53d6dc3cf0b17..219c94b5a895d 100644 --- a/pandas/tests/io/formats/test_to_latex.py +++ b/pandas/tests/io/formats/test_to_latex.py @@ -124,6 +124,24 @@ def test_to_latex_column_format(self): ) assert result == expected + def test_to_latex_float_format_object_col(self): + # GH#40024 + ser = Series([1000.0, "test"]) + result = ser.to_latex(float_format="{:,.0f}".format) + expected = _dedent( + r""" + \begin{tabular}{ll} + \toprule + {} & 0 \\ + \midrule + 0 & 1,000 \\ + 1 & test \\ + \bottomrule + \end{tabular} + """ + ) + assert result == expected + def test_to_latex_empty_tabular(self): df = DataFrame() result = df.to_latex()