From 833ca697b924a55eaa680b786e43b783db7a384b Mon Sep 17 00:00:00 2001 From: saldanhad Date: Tue, 1 Oct 2024 17:45:20 +0530 Subject: [PATCH 01/13] fix html display float/strings --- pandas/core/frame.py | 3 ++- pandas/io/formats/html.py | 2 +- pandas/tests/frame/test_info_repr_html.py | 32 +++++++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 pandas/tests/frame/test_info_repr_html.py 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/io/formats/html.py b/pandas/io/formats/html.py index adaeed017d7bf..618301d964a28 100644 --- a/pandas/io/formats/html.py +++ b/pandas/io/formats/html.py @@ -190,7 +190,7 @@ def _write_cell( if self.escape: # escape & first to prevent double escaping of & - esc = {"&": r"&", "<": r"<", ">": r">"} + esc = {"&": r"&", "<": r"<", ">": r">"," ":" "} else: esc = {} diff --git a/pandas/tests/frame/test_info_repr_html.py b/pandas/tests/frame/test_info_repr_html.py new file mode 100644 index 0000000000000..b44a02346964b --- /dev/null +++ b/pandas/tests/frame/test_info_repr_html.py @@ -0,0 +1,32 @@ +import pytest +import pandas as pd + + +class Testfloatformat: + @pytest.mark.parametrize("data, format_option, expected_values", [ + ({"A": [12345.6789]}, "{:12.3f}", "   12345.679"), + ({"A": [None]}, "{:.3f}", " None"), + ({"A": [""]}, "{:.2f}", " "), + ({"A": [112345.6789]}, "{:6.3f}", "112345.679"), + ({"A": ["foo foo"]}, None, " foo    foo"), + ({"A": [None]}, None, " None"), + ({"A": ["foo foo foo"]}, None, " foo foo foo"), + ]) #test cases + + def test_float_formatting_html_output(self,data,format_option, expected_values): + # set float format, avoid for string checks + if format_option is not None: + pd.set_option("display.float_format", format_option.format) + + # crate dataframe + df = pd.DataFrame(data) + + # capture html output + html_output = df._repr_html_() + + # check + assert expected_values in html_output + + # reset option + if format_option is not None: + pd.reset_option("display.float_format") \ No newline at end of file From 0613ae8f5a2f983db6dcfbc08ea1a777d4b483b3 Mon Sep 17 00:00:00 2001 From: saldanhad Date: Tue, 1 Oct 2024 18:14:07 +0530 Subject: [PATCH 02/13] add test under io, update whatsnew --- doc/source/whatsnew/v3.0.0.rst | 2 ++ pandas/tests/{frame => io}/test_info_repr_html.py | 0 2 files changed, 2 insertions(+) rename pandas/tests/{frame => io}/test_info_repr_html.py (100%) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 41ba80989a0ce..9a214adac9408 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -636,6 +636,8 @@ I/O - Bug in :meth:`read_stata` raising ``KeyError`` when input file is stored in big-endian format and contains strL data. (:issue:`58638`) - Bug in :meth:`read_stata` where extreme value integers were incorrectly interpreted as missing for format versions 111 and prior (:issue:`58130`) - Bug in :meth:`read_stata` where the missing code for double was not recognised for format versions 105 and prior (:issue:`58149`) +- Bug in :meth:`DataFrame._repr_html_` pass :func:`get_option("display.float_format")` to :class:`DataFrameFormatter`, such that HTML output respects the configured float formatting (:issue:`59876`) +- Bug in :class:`HTMLFormatter._write_cell` to escape regular spaces as ` `, ensuring multiple spaces are preserved in the HTML output (:issue:`59876`). Period ^^^^^^ diff --git a/pandas/tests/frame/test_info_repr_html.py b/pandas/tests/io/test_info_repr_html.py similarity index 100% rename from pandas/tests/frame/test_info_repr_html.py rename to pandas/tests/io/test_info_repr_html.py From 1061442e0a1cf8f745b0863762f2aa023d388336 Mon Sep 17 00:00:00 2001 From: saldanhad Date: Tue, 1 Oct 2024 18:25:01 +0530 Subject: [PATCH 03/13] fix linting --- doc/source/whatsnew/v3.0.0.rst | 4 +- pandas/io/formats/html.py | 2 +- pandas/tests/io/test_info_repr_html.py | 51 ++++++++++++++------------ 3 files changed, 30 insertions(+), 27 deletions(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 9a214adac9408..4804498fbb71e 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -619,7 +619,9 @@ MultiIndex I/O ^^^ - Bug in :class:`DataFrame` and :class:`Series` ``repr`` of :py:class:`collections.abc.Mapping`` elements. (:issue:`57915`) +- Bug in :class:`HTMLFormatter._write_cell` to escape regular spaces as `` `` ensuring multiple spaces are preserved in the HTML output. (:issue:`59876`) - 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_` pass :func: ``get_option("display.float_format")`` to :class:`DataFrameFormatter`, such that HTML output respects the configured float formatting. (: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`) @@ -636,8 +638,6 @@ I/O - Bug in :meth:`read_stata` raising ``KeyError`` when input file is stored in big-endian format and contains strL data. (:issue:`58638`) - Bug in :meth:`read_stata` where extreme value integers were incorrectly interpreted as missing for format versions 111 and prior (:issue:`58130`) - Bug in :meth:`read_stata` where the missing code for double was not recognised for format versions 105 and prior (:issue:`58149`) -- Bug in :meth:`DataFrame._repr_html_` pass :func:`get_option("display.float_format")` to :class:`DataFrameFormatter`, such that HTML output respects the configured float formatting (:issue:`59876`) -- Bug in :class:`HTMLFormatter._write_cell` to escape regular spaces as ` `, ensuring multiple spaces are preserved in the HTML output (:issue:`59876`). Period ^^^^^^ diff --git a/pandas/io/formats/html.py b/pandas/io/formats/html.py index 618301d964a28..6f98885950618 100644 --- a/pandas/io/formats/html.py +++ b/pandas/io/formats/html.py @@ -190,7 +190,7 @@ def _write_cell( if self.escape: # escape & first to prevent double escaping of & - esc = {"&": r"&", "<": r"<", ">": r">"," ":" "} + esc = {"&": r"&", "<": r"<", ">": r">", " ": " "} else: esc = {} diff --git a/pandas/tests/io/test_info_repr_html.py b/pandas/tests/io/test_info_repr_html.py index b44a02346964b..348c6b8eeb08d 100644 --- a/pandas/tests/io/test_info_repr_html.py +++ b/pandas/tests/io/test_info_repr_html.py @@ -1,32 +1,35 @@ import pytest + import pandas as pd class Testfloatformat: - @pytest.mark.parametrize("data, format_option, expected_values", [ - ({"A": [12345.6789]}, "{:12.3f}", "   12345.679"), - ({"A": [None]}, "{:.3f}", " None"), - ({"A": [""]}, "{:.2f}", " "), - ({"A": [112345.6789]}, "{:6.3f}", "112345.679"), - ({"A": ["foo foo"]}, None, " foo    foo"), - ({"A": [None]}, None, " None"), - ({"A": ["foo foo foo"]}, None, " foo foo foo"), - ]) #test cases + @pytest.mark.parametrize( + "data, format_option, expected_values", + [ + ({"A": [12345.6789]}, "{:12.3f}", "   12345.679"), + ({"A": [None]}, "{:.3f}", " None"), + ({"A": [""]}, "{:.2f}", " "), + ({"A": [112345.6789]}, "{:6.3f}", "112345.679"), + ({"A": ["foo foo"]}, None, " foo    foo"), + ({"A": [None]}, None, " None"), + ({"A": ["foo foo foo"]}, None, " foo foo foo"), + ], + ) # test cases + def test_float_formatting_html_output(self, data, format_option, expected_values): + # set float format, avoid for string checks + if format_option is not None: + pd.set_option("display.float_format", format_option.format) + + # create dataframe + df = pd.DataFrame(data) - def test_float_formatting_html_output(self,data,format_option, expected_values): - # set float format, avoid for string checks - if format_option is not None: - pd.set_option("display.float_format", format_option.format) - - # crate dataframe - df = pd.DataFrame(data) - - # capture html output - html_output = df._repr_html_() + # capture html output + html_output = df._repr_html_() - # check - assert expected_values in html_output + # check + assert expected_values in html_output - # reset option - if format_option is not None: - pd.reset_option("display.float_format") \ No newline at end of file + # reset option + if format_option is not None: + pd.reset_option("display.float_format") From 1b612f07d8605d6eeb3826a2c756370558e6408f Mon Sep 17 00:00:00 2001 From: saldanhad Date: Tue, 1 Oct 2024 22:58:11 +0530 Subject: [PATCH 04/13] changes to fix floats only --- doc/source/whatsnew/v3.0.0.rst | 1 - pandas/io/formats/html.py | 2 +- pandas/tests/io/{ => formats}/test_info_repr_html.py | 5 +---- 3 files changed, 2 insertions(+), 6 deletions(-) rename pandas/tests/io/{ => formats}/test_info_repr_html.py (75%) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 4804498fbb71e..52751b675c7a6 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -619,7 +619,6 @@ MultiIndex I/O ^^^ - Bug in :class:`DataFrame` and :class:`Series` ``repr`` of :py:class:`collections.abc.Mapping`` elements. (:issue:`57915`) -- Bug in :class:`HTMLFormatter._write_cell` to escape regular spaces as `` `` ensuring multiple spaces are preserved in the HTML output. (:issue:`59876`) - 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_` pass :func: ``get_option("display.float_format")`` to :class:`DataFrameFormatter`, such that HTML output respects the configured float formatting. (: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`) diff --git a/pandas/io/formats/html.py b/pandas/io/formats/html.py index 6f98885950618..adaeed017d7bf 100644 --- a/pandas/io/formats/html.py +++ b/pandas/io/formats/html.py @@ -190,7 +190,7 @@ def _write_cell( if self.escape: # escape & first to prevent double escaping of & - esc = {"&": r"&", "<": r"<", ">": r">", " ": " "} + esc = {"&": r"&", "<": r"<", ">": r">"} else: esc = {} diff --git a/pandas/tests/io/test_info_repr_html.py b/pandas/tests/io/formats/test_info_repr_html.py similarity index 75% rename from pandas/tests/io/test_info_repr_html.py rename to pandas/tests/io/formats/test_info_repr_html.py index 348c6b8eeb08d..4eda7f748ba2a 100644 --- a/pandas/tests/io/test_info_repr_html.py +++ b/pandas/tests/io/formats/test_info_repr_html.py @@ -7,13 +7,10 @@ class Testfloatformat: @pytest.mark.parametrize( "data, format_option, expected_values", [ - ({"A": [12345.6789]}, "{:12.3f}", "   12345.679"), + ({"A": [12345.6789]}, "{:12.3f}", "12345.679"), ({"A": [None]}, "{:.3f}", " None"), ({"A": [""]}, "{:.2f}", " "), ({"A": [112345.6789]}, "{:6.3f}", "112345.679"), - ({"A": ["foo foo"]}, None, " foo    foo"), - ({"A": [None]}, None, " None"), - ({"A": ["foo foo foo"]}, None, " foo foo foo"), ], ) # test cases def test_float_formatting_html_output(self, data, format_option, expected_values): From e1cf343c54719d7f2984570474b85f634d8118f4 Mon Sep 17 00:00:00 2001 From: saldanhad Date: Tue, 1 Oct 2024 23:11:54 +0530 Subject: [PATCH 05/13] Revert "fix linting" This reverts commit 1061442e0a1cf8f745b0863762f2aa023d388336. --- doc/source/whatsnew/v3.0.0.rst | 4 +- pandas/io/formats/html.py | 2 +- pandas/tests/io/test_info_repr_html.py | 51 ++++++++++++-------------- 3 files changed, 27 insertions(+), 30 deletions(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 4804498fbb71e..9a214adac9408 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -619,9 +619,7 @@ MultiIndex I/O ^^^ - Bug in :class:`DataFrame` and :class:`Series` ``repr`` of :py:class:`collections.abc.Mapping`` elements. (:issue:`57915`) -- Bug in :class:`HTMLFormatter._write_cell` to escape regular spaces as `` `` ensuring multiple spaces are preserved in the HTML output. (:issue:`59876`) - 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_` pass :func: ``get_option("display.float_format")`` to :class:`DataFrameFormatter`, such that HTML output respects the configured float formatting. (: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`) @@ -638,6 +636,8 @@ I/O - Bug in :meth:`read_stata` raising ``KeyError`` when input file is stored in big-endian format and contains strL data. (:issue:`58638`) - Bug in :meth:`read_stata` where extreme value integers were incorrectly interpreted as missing for format versions 111 and prior (:issue:`58130`) - Bug in :meth:`read_stata` where the missing code for double was not recognised for format versions 105 and prior (:issue:`58149`) +- Bug in :meth:`DataFrame._repr_html_` pass :func:`get_option("display.float_format")` to :class:`DataFrameFormatter`, such that HTML output respects the configured float formatting (:issue:`59876`) +- Bug in :class:`HTMLFormatter._write_cell` to escape regular spaces as ` `, ensuring multiple spaces are preserved in the HTML output (:issue:`59876`). Period ^^^^^^ diff --git a/pandas/io/formats/html.py b/pandas/io/formats/html.py index 6f98885950618..618301d964a28 100644 --- a/pandas/io/formats/html.py +++ b/pandas/io/formats/html.py @@ -190,7 +190,7 @@ def _write_cell( if self.escape: # escape & first to prevent double escaping of & - esc = {"&": r"&", "<": r"<", ">": r">", " ": " "} + esc = {"&": r"&", "<": r"<", ">": r">"," ":" "} else: esc = {} diff --git a/pandas/tests/io/test_info_repr_html.py b/pandas/tests/io/test_info_repr_html.py index 348c6b8eeb08d..b44a02346964b 100644 --- a/pandas/tests/io/test_info_repr_html.py +++ b/pandas/tests/io/test_info_repr_html.py @@ -1,35 +1,32 @@ import pytest - import pandas as pd class Testfloatformat: - @pytest.mark.parametrize( - "data, format_option, expected_values", - [ - ({"A": [12345.6789]}, "{:12.3f}", "   12345.679"), - ({"A": [None]}, "{:.3f}", " None"), - ({"A": [""]}, "{:.2f}", " "), - ({"A": [112345.6789]}, "{:6.3f}", "112345.679"), - ({"A": ["foo foo"]}, None, " foo    foo"), - ({"A": [None]}, None, " None"), - ({"A": ["foo foo foo"]}, None, " foo foo foo"), - ], - ) # test cases - def test_float_formatting_html_output(self, data, format_option, expected_values): - # set float format, avoid for string checks - if format_option is not None: - pd.set_option("display.float_format", format_option.format) - - # create dataframe - df = pd.DataFrame(data) + @pytest.mark.parametrize("data, format_option, expected_values", [ + ({"A": [12345.6789]}, "{:12.3f}", "   12345.679"), + ({"A": [None]}, "{:.3f}", " None"), + ({"A": [""]}, "{:.2f}", " "), + ({"A": [112345.6789]}, "{:6.3f}", "112345.679"), + ({"A": ["foo foo"]}, None, " foo    foo"), + ({"A": [None]}, None, " None"), + ({"A": ["foo foo foo"]}, None, " foo foo foo"), + ]) #test cases - # capture html output - html_output = df._repr_html_() + def test_float_formatting_html_output(self,data,format_option, expected_values): + # set float format, avoid for string checks + if format_option is not None: + pd.set_option("display.float_format", format_option.format) + + # crate dataframe + df = pd.DataFrame(data) + + # capture html output + html_output = df._repr_html_() - # check - assert expected_values in html_output + # check + assert expected_values in html_output - # reset option - if format_option is not None: - pd.reset_option("display.float_format") + # reset option + if format_option is not None: + pd.reset_option("display.float_format") \ No newline at end of file From 06b55dcb33f7422ecfa6d64913fe290de6f068b7 Mon Sep 17 00:00:00 2001 From: saldanhad Date: Wed, 2 Oct 2024 01:45:58 +0530 Subject: [PATCH 06/13] test script for float format --- pandas/io/formats/html.py | 4 +++ .../tests/io/formats/test_info_repr_html.py | 32 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 pandas/tests/io/formats/test_info_repr_html.py diff --git a/pandas/io/formats/html.py b/pandas/io/formats/html.py index 618301d964a28..95e7af86cb7d7 100644 --- a/pandas/io/formats/html.py +++ b/pandas/io/formats/html.py @@ -190,7 +190,11 @@ def _write_cell( if self.escape: # escape & first to prevent double escaping of & +<<<<<<< Updated upstream esc = {"&": r"&", "<": r"<", ">": r">"," ":" "} +======= + esc = {"&": r"&", "<": r"<", ">": r">"} +>>>>>>> Stashed changes else: esc = {} diff --git a/pandas/tests/io/formats/test_info_repr_html.py b/pandas/tests/io/formats/test_info_repr_html.py new file mode 100644 index 0000000000000..f3ea1e26b133e --- /dev/null +++ b/pandas/tests/io/formats/test_info_repr_html.py @@ -0,0 +1,32 @@ +import pytest + +import pandas as pd + + +class Testfloatformat: + @pytest.mark.parametrize( + "data, format_option, expected_values", + [ + ({"A": [12345.6789]}, "{:12.3f}", "12345.679"), + ({"A": [None]}, "{:.3f}", "None"), + ({"A": [""]}, "{:.2f}", ""), + ({"A": [112345.6789]}, "{:6.3f}", "112345.679"), + ], + ) # test cases + def test_float_formatting_html_output(self, data, format_option, expected_values): + # set float format, avoid for string checks + if format_option is not None: + pd.set_option("display.float_format", format_option.format) + + # create dataframe + df = pd.DataFrame(data) + + # capture html output + html_output = df._repr_html_() + + # check + assert expected_values in html_output + + # reset option + if format_option is not None: + pd.reset_option("display.float_format") From e5f11e0801db84b797d329924550a54e61d4ecd0 Mon Sep 17 00:00:00 2001 From: saldanhad Date: Wed, 2 Oct 2024 02:18:57 +0530 Subject: [PATCH 07/13] remove nbsp implementation, keep floats --- doc/source/whatsnew/v3.0.0.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 8c007eb7488aa..a44d30d8bcb10 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -620,6 +620,8 @@ 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_` pass :func: ``get_option("display.float_format")`` to :class:`DataFrameFormatter`, such that HTML output respects the configured float formatting. (:issue:`59876`) +- Bug in :meth:`DataFrame._repr_html_` pass :func:`get_option("display.float_format")` to :class:`DataFrameFormatter`, such that HTML output respects the configured float formatting (: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`) @@ -636,8 +638,6 @@ I/O - Bug in :meth:`read_stata` raising ``KeyError`` when input file is stored in big-endian format and contains strL data. (:issue:`58638`) - Bug in :meth:`read_stata` where extreme value integers were incorrectly interpreted as missing for format versions 111 and prior (:issue:`58130`) - Bug in :meth:`read_stata` where the missing code for double was not recognised for format versions 105 and prior (:issue:`58149`) -- Bug in :meth:`DataFrame._repr_html_` pass :func:`get_option("display.float_format")` to :class:`DataFrameFormatter`, such that HTML output respects the configured float formatting (:issue:`59876`) -- Bug in :class:`HTMLFormatter._write_cell` to escape regular spaces as ` `, ensuring multiple spaces are preserved in the HTML output (:issue:`59876`). Period ^^^^^^ From c619b3ccb4e8a42f2d6e00f3768386008fb24113 Mon Sep 17 00:00:00 2001 From: saldanhad Date: Wed, 2 Oct 2024 06:17:43 +0530 Subject: [PATCH 08/13] Trigger CI From bb70d6e2e8ac71b1a11ccbc63773d864e8f17ebd Mon Sep 17 00:00:00 2001 From: saldanhad Date: Thu, 3 Oct 2024 23:08:52 +0530 Subject: [PATCH 09/13] implement changes post review --- doc/source/whatsnew/v3.0.0.rst | 3 +- pandas/tests/io/formats/test_format.py | 22 +++++++++++++ .../tests/io/formats/test_info_repr_html.py | 32 ------------------- 3 files changed, 23 insertions(+), 34 deletions(-) delete mode 100644 pandas/tests/io/formats/test_info_repr_html.py diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index a44d30d8bcb10..baf5385021f65 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -620,8 +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_` pass :func: ``get_option("display.float_format")`` to :class:`DataFrameFormatter`, such that HTML output respects the configured float formatting. (:issue:`59876`) -- Bug in :meth:`DataFrame._repr_html_` pass :func:`get_option("display.float_format")` to :class:`DataFrameFormatter`, such that HTML output respects the configured float formatting (:issue:`59876`) +- 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/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index af7b04d66096a..250c6053f4f3e 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -26,6 +26,7 @@ option_context, read_csv, reset_option, + set_option, ) from pandas.io.formats import printing @@ -368,6 +369,27 @@ 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", + [ + ({"A": [12345.6789]}, "{:12.3f}", "12345.679"), + ({"A": [None]}, "{:.3f}", "None"), + ({"A": [""]}, "{:.2f}", ""), + ({"A": [112345.6789]}, "{:6.3f}", "112345.679"), + ], + ) + def test_repr_float_formatting_html_output(self, data, format_option, expected_values): + if format_option is not None: + set_option("display.float_format", format_option.format) + + df = DataFrame(data) + html_output = df._repr_html_() + assert expected_values in html_output + + # reset option + if format_option is not None: + reset_option("display.float_format") + def test_str_max_colwidth(self): # GH 7856 df = DataFrame( diff --git a/pandas/tests/io/formats/test_info_repr_html.py b/pandas/tests/io/formats/test_info_repr_html.py deleted file mode 100644 index f3ea1e26b133e..0000000000000 --- a/pandas/tests/io/formats/test_info_repr_html.py +++ /dev/null @@ -1,32 +0,0 @@ -import pytest - -import pandas as pd - - -class Testfloatformat: - @pytest.mark.parametrize( - "data, format_option, expected_values", - [ - ({"A": [12345.6789]}, "{:12.3f}", "12345.679"), - ({"A": [None]}, "{:.3f}", "None"), - ({"A": [""]}, "{:.2f}", ""), - ({"A": [112345.6789]}, "{:6.3f}", "112345.679"), - ], - ) # test cases - def test_float_formatting_html_output(self, data, format_option, expected_values): - # set float format, avoid for string checks - if format_option is not None: - pd.set_option("display.float_format", format_option.format) - - # create dataframe - df = pd.DataFrame(data) - - # capture html output - html_output = df._repr_html_() - - # check - assert expected_values in html_output - - # reset option - if format_option is not None: - pd.reset_option("display.float_format") From 14de22f29c5aedd4c260bb4b6bf618e71da9bbfb Mon Sep 17 00:00:00 2001 From: saldanhad Date: Thu, 3 Oct 2024 23:12:05 +0530 Subject: [PATCH 10/13] lint check --- pandas/tests/io/formats/test_format.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index 250c6053f4f3e..8a7939f555e12 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -377,8 +377,10 @@ def test_repr_min_rows(self): ({"A": [""]}, "{:.2f}", ""), ({"A": [112345.6789]}, "{:6.3f}", "112345.679"), ], - ) - def test_repr_float_formatting_html_output(self, data, format_option, expected_values): + ) + def test_repr_float_formatting_html_output( + self, data, format_option, expected_values + ): if format_option is not None: set_option("display.float_format", format_option.format) From fb4c5fde478324bb213138110d5186a5df306545 Mon Sep 17 00:00:00 2001 From: saldanhad Date: Thu, 3 Oct 2024 23:32:10 +0530 Subject: [PATCH 11/13] update test_formats.py --- pandas/tests/io/formats/test_format.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index 8a7939f555e12..ef2178f7d6e86 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -26,7 +26,6 @@ option_context, read_csv, reset_option, - set_option, ) from pandas.io.formats import printing @@ -382,15 +381,10 @@ def test_repr_float_formatting_html_output( self, data, format_option, expected_values ): if format_option is not None: - set_option("display.float_format", format_option.format) - - df = DataFrame(data) - html_output = df._repr_html_() - assert expected_values in html_output - - # reset option - if format_option is not None: - reset_option("display.float_format") + with option_context("display.float_format", format_option.format): + df = DataFrame(data) + html_output = df._repr_html_() + assert expected_values in html_output def test_str_max_colwidth(self): # GH 7856 From 0e1984754097622cbb0545f8c8988d26fa922fc9 Mon Sep 17 00:00:00 2001 From: saldanhad Date: Thu, 3 Oct 2024 23:44:28 +0530 Subject: [PATCH 12/13] rfc test_format.py --- pandas/tests/io/formats/test_format.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index ef2178f7d6e86..0127258d76745 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -380,11 +380,10 @@ def test_repr_min_rows(self): def test_repr_float_formatting_html_output( self, data, format_option, expected_values ): - if format_option is not None: - with option_context("display.float_format", format_option.format): - df = DataFrame(data) - html_output = df._repr_html_() - assert expected_values in html_output + with option_context("display.float_format", format_option.format): + df = DataFrame(data) + html_output = df._repr_html_() + assert expected_values in html_output def test_str_max_colwidth(self): # GH 7856 From 9f5572338cac4b237e80c0832adefbfaba651d9a Mon Sep 17 00:00:00 2001 From: saldanhad Date: Fri, 4 Oct 2024 01:10:31 +0530 Subject: [PATCH 13/13] update test cases --- pandas/tests/io/formats/test_format.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index 0127258d76745..82cc3a838ca68 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -371,17 +371,17 @@ def test_repr_min_rows(self): @pytest.mark.parametrize( "data, format_option, expected_values", [ - ({"A": [12345.6789]}, "{:12.3f}", "12345.679"), - ({"A": [None]}, "{:.3f}", "None"), - ({"A": [""]}, "{:.2f}", ""), - ({"A": [112345.6789]}, "{:6.3f}", "112345.679"), + (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(data) + df = DataFrame({"A": [data]}) html_output = df._repr_html_() assert expected_values in html_output