From e5ab2cbd16211f02c13bf5711fdf2e13a30f187b Mon Sep 17 00:00:00 2001 From: Terji Petersen Date: Wed, 15 Feb 2023 18:52:09 +0000 Subject: [PATCH 1/3] BUG: DataFrame.to_html(na_rep=-) with non-scalar data --- doc/source/whatsnew/v2.0.0.rst | 1 + pandas/io/formats/style_render.py | 2 +- .../data/html/gh47103_expected_output.html | 18 ++++++++++++++++++ pandas/tests/io/formats/test_to_html.py | 8 ++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 pandas/tests/io/formats/data/html/gh47103_expected_output.html diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 7fc856be374e9..b3f7f217aa901 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -1322,6 +1322,7 @@ I/O - Bug in :func:`read_csv` unnecessarily overflowing for extension array dtype when containing ``NA`` (:issue:`32134`) - Bug in :meth:`DataFrame.to_dict` not converting ``NA`` to ``None`` (:issue:`50795`) - Bug in :meth:`DataFrame.to_json` where it would segfault when failing to encode a string (:issue:`50307`) +- Bug in :meth:`DataFrame.to_html` with `na_rep` set when the :class:`DataFrame` contains non-scalar data (:issue:`47103`) - Bug in :func:`read_xml` where file-like objects failed when iterparse is used (:issue:`50641`) - Bug in :func:`read_xml` ignored repeated elements when iterparse is used (:issue:`51183`) diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py index 6ae9c528e4772..141b9a70023b6 100644 --- a/pandas/io/formats/style_render.py +++ b/pandas/io/formats/style_render.py @@ -1814,7 +1814,7 @@ def _maybe_wrap_formatter( if na_rep is None: return func_3 else: - return lambda x: na_rep if isna(x) else func_3(x) + return lambda x: na_rep if (isna(x) is True) else func_3(x) def non_reducing_slice(slice_: Subset): diff --git a/pandas/tests/io/formats/data/html/gh47103_expected_output.html b/pandas/tests/io/formats/data/html/gh47103_expected_output.html new file mode 100644 index 0000000000000..f3f4f57a31a23 --- /dev/null +++ b/pandas/tests/io/formats/data/html/gh47103_expected_output.html @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + +
 ab
01[1, 2, 3]
diff --git a/pandas/tests/io/formats/test_to_html.py b/pandas/tests/io/formats/test_to_html.py index 3059efef09095..45fefa8fbeeec 100644 --- a/pandas/tests/io/formats/test_to_html.py +++ b/pandas/tests/io/formats/test_to_html.py @@ -882,6 +882,14 @@ def test_to_html_na_rep_and_float_format(na_rep, datapath): assert result == expected +def test_to_html_na_rep_non_scalar_data(datapath): + # GH47103 + df = pd.DataFrame([dict(a=1, b=[1, 2, 3])]) + result = df.style.format(na_rep="-").to_html(table_uuid="test") + expected = expected_html(datapath, "gh47103_expected_output") + "\n" + assert result == expected + + def test_to_html_float_format_object_col(datapath): # GH#40024 df = DataFrame(data={"x": [1000.0, "test"]}) From d35c08693fdb21fbf01a4c40f2c106cceff6a35d Mon Sep 17 00:00:00 2001 From: Terji Petersen Date: Wed, 15 Feb 2023 21:27:13 +0000 Subject: [PATCH 2/3] fixes --- doc/source/whatsnew/v2.0.0.rst | 2 +- .../data/html/gh47103_expected_output.html | 18 +++++------- pandas/tests/io/formats/style/test_html.py | 29 +++++++++++++++++++ pandas/tests/io/formats/test_to_html.py | 4 +-- 4 files changed, 40 insertions(+), 13 deletions(-) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index b3f7f217aa901..e9e06044eba0a 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -1322,7 +1322,7 @@ I/O - Bug in :func:`read_csv` unnecessarily overflowing for extension array dtype when containing ``NA`` (:issue:`32134`) - Bug in :meth:`DataFrame.to_dict` not converting ``NA`` to ``None`` (:issue:`50795`) - Bug in :meth:`DataFrame.to_json` where it would segfault when failing to encode a string (:issue:`50307`) -- Bug in :meth:`DataFrame.to_html` with `na_rep` set when the :class:`DataFrame` contains non-scalar data (:issue:`47103`) +- Bug in :meth:`DataFrame.to_html` with ``na_rep`` set when the :class:`DataFrame` contains non-scalar data (:issue:`47103`) - Bug in :func:`read_xml` where file-like objects failed when iterparse is used (:issue:`50641`) - Bug in :func:`read_xml` ignored repeated elements when iterparse is used (:issue:`51183`) diff --git a/pandas/tests/io/formats/data/html/gh47103_expected_output.html b/pandas/tests/io/formats/data/html/gh47103_expected_output.html index f3f4f57a31a23..f8c54cb85c815 100644 --- a/pandas/tests/io/formats/data/html/gh47103_expected_output.html +++ b/pandas/tests/io/formats/data/html/gh47103_expected_output.html @@ -1,18 +1,16 @@ - - +
- - - - + + + + - - - + + +
 ab
ab
01[1, 2, 3]01[1, 2, 3]
diff --git a/pandas/tests/io/formats/style/test_html.py b/pandas/tests/io/formats/style/test_html.py index d878d82f55e51..1867260fbc4b4 100644 --- a/pandas/tests/io/formats/style/test_html.py +++ b/pandas/tests/io/formats/style/test_html.py @@ -976,3 +976,32 @@ def html_lines(foot_prefix: str): ) ) assert expected_css + expected_table == result + + +def test_to_html_na_rep_non_scalar_data(datapath): + # GH47103 + df = DataFrame([dict(a=1, b=[1, 2, 3], c=np.nan)]) + result = df.style.format(na_rep="-").to_html(table_uuid="test") + expected = """\ + + + + + + + + + + + + + + + + + + +
 abc
01[1, 2, 3]-
+""" + assert result == expected diff --git a/pandas/tests/io/formats/test_to_html.py b/pandas/tests/io/formats/test_to_html.py index 45fefa8fbeeec..4dbb3b56d7677 100644 --- a/pandas/tests/io/formats/test_to_html.py +++ b/pandas/tests/io/formats/test_to_html.py @@ -884,8 +884,8 @@ def test_to_html_na_rep_and_float_format(na_rep, datapath): def test_to_html_na_rep_non_scalar_data(datapath): # GH47103 - df = pd.DataFrame([dict(a=1, b=[1, 2, 3])]) - result = df.style.format(na_rep="-").to_html(table_uuid="test") + df = DataFrame([dict(a=1, b=[1, 2, 3])]) + result = df.to_html(na_rep="-") expected = expected_html(datapath, "gh47103_expected_output") + "\n" assert result == expected From 97e2831d1722f74ad5ccc67f083a2cbba2b6b266 Mon Sep 17 00:00:00 2001 From: Terji Petersen Date: Wed, 15 Feb 2023 23:35:44 +0000 Subject: [PATCH 3/3] fix test --- pandas/tests/io/formats/test_to_html.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/io/formats/test_to_html.py b/pandas/tests/io/formats/test_to_html.py index 4dbb3b56d7677..a66858d3f983e 100644 --- a/pandas/tests/io/formats/test_to_html.py +++ b/pandas/tests/io/formats/test_to_html.py @@ -886,7 +886,7 @@ def test_to_html_na_rep_non_scalar_data(datapath): # GH47103 df = DataFrame([dict(a=1, b=[1, 2, 3])]) result = df.to_html(na_rep="-") - expected = expected_html(datapath, "gh47103_expected_output") + "\n" + expected = expected_html(datapath, "gh47103_expected_output") assert result == expected