From 236df37d75eee98c6d76e593dfd7ef133e1dbf5c Mon Sep 17 00:00:00 2001 From: Matthew Zeitlin Date: Fri, 9 Apr 2021 11:11:16 -0400 Subject: [PATCH 1/7] REGR: object column repr not respecting float format --- pandas/io/formats/format.py | 7 +++++-- pandas/tests/io/formats/test_format.py | 15 ++++++++++++++ pandas/tests/io/formats/test_to_html.py | 26 +++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 9b7d4d92d75d1..279f4adab3ce8 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,8 @@ 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..9f67300fe7758 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 pd.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..d50e4051cd8a3 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: "{:,.0f}".format(x)) + expected = """ + + + + + + + + + + + + + + + + +
x
01,000
1test
""" + + assert result == expected From 58a4bc4da97d8c04ebe50226702e4502d19425b7 Mon Sep 17 00:00:00 2001 From: Matthew Zeitlin Date: Fri, 9 Apr 2021 11:16:38 -0400 Subject: [PATCH 2/7] Add whatsnew --- doc/source/whatsnew/v1.2.4.rst | 2 +- pandas/tests/io/formats/test_format.py | 2 +- pandas/tests/io/formats/test_to_html.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v1.2.4.rst b/doc/source/whatsnew/v1.2.4.rst index 9cef1307278e8..f95c88561bbfa 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`` (:issue:`40024`) .. --------------------------------------------------------------------------- diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index 9f67300fe7758..c6155cac101e6 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -2035,7 +2035,7 @@ def test_repr_str_float_truncation(self, data, expected): def test_repr_float_format_in_object_col(self, float_format, expected): # GH#40024 df = Series([1000.0, "test"]) - with pd.option_context("display.float_format", float_format): + with option_context("display.float_format", float_format): result = repr(df) assert result == expected diff --git a/pandas/tests/io/formats/test_to_html.py b/pandas/tests/io/formats/test_to_html.py index d50e4051cd8a3..ec2f109900f3a 100644 --- a/pandas/tests/io/formats/test_to_html.py +++ b/pandas/tests/io/formats/test_to_html.py @@ -888,7 +888,7 @@ def test_to_html_na_rep_and_float_format(na_rep): 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: "{:,.0f}".format(x)) + result = df.to_html(float_format=lambda x: f"{x:,.0f}") expected = """ From 52639d7c0e6c39b0a596593b14e4f893f9e8eda0 Mon Sep 17 00:00:00 2001 From: Matthew Zeitlin Date: Fri, 9 Apr 2021 11:23:41 -0400 Subject: [PATCH 3/7] Remove whitespace --- pandas/io/formats/format.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 279f4adab3ce8..ba406a1ef117c 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -1333,7 +1333,6 @@ 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(float_format(v)) else: if leading_space is False: From cf5c528647b198380eedaafd548ab68f09dc6c30 Mon Sep 17 00:00:00 2001 From: Matthew Zeitlin Date: Fri, 9 Apr 2021 16:20:04 -0400 Subject: [PATCH 4/7] Update whatsnew --- doc/source/whatsnew/v1.2.4.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.2.4.rst b/doc/source/whatsnew/v1.2.4.rst index f95c88561bbfa..1c06ed65f402c 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`` (:issue:`40024`) +- 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_html` and :meth:`DataFrame.to_latex` (:issue:`40024`) .. --------------------------------------------------------------------------- From 7c9b13d97a4f34a0ecb8fcf67b10dd4da5fb84ea Mon Sep 17 00:00:00 2001 From: Matthew Zeitlin Date: Fri, 9 Apr 2021 16:48:15 -0400 Subject: [PATCH 5/7] Add test for to_latex --- pandas/tests/io/formats/test_to_latex.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pandas/tests/io/formats/test_to_latex.py b/pandas/tests/io/formats/test_to_latex.py index 53d6dc3cf0b17..71c217eac2e92 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 = pd.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() From e06e55d029026c1c69bb47cd7da4db817e968807 Mon Sep 17 00:00:00 2001 From: Matthew Zeitlin Date: Fri, 9 Apr 2021 16:48:52 -0400 Subject: [PATCH 6/7] precommit fixup --- pandas/tests/io/formats/test_to_latex.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/io/formats/test_to_latex.py b/pandas/tests/io/formats/test_to_latex.py index 71c217eac2e92..219c94b5a895d 100644 --- a/pandas/tests/io/formats/test_to_latex.py +++ b/pandas/tests/io/formats/test_to_latex.py @@ -126,7 +126,7 @@ def test_to_latex_column_format(self): def test_to_latex_float_format_object_col(self): # GH#40024 - ser = pd.Series([1000.0, "test"]) + ser = Series([1000.0, "test"]) result = ser.to_latex(float_format="{:,.0f}".format) expected = _dedent( r""" From d051b0046762b678b4076c69fbeefa60b4959f9b Mon Sep 17 00:00:00 2001 From: Matthew Zeitlin Date: Sat, 10 Apr 2021 10:34:19 -0400 Subject: [PATCH 7/7] Reference to_string as well --- doc/source/whatsnew/v1.2.4.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.2.4.rst b/doc/source/whatsnew/v1.2.4.rst index 1c06ed65f402c..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_html` and :meth:`DataFrame.to_latex` (:issue:`40024`) +- 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`) .. ---------------------------------------------------------------------------