From ca43cd4acfa68937011ed10a02f04231218bf4b2 Mon Sep 17 00:00:00 2001 From: Soumyadip Mal Date: Mon, 7 Aug 2023 22:12:08 +0530 Subject: [PATCH 1/9] Fresh new feature --- pandas/io/formats/format.py | 2 ++ .../io/formats/data/html/gh54167_expected_output.html | 10 ++++++++++ pandas/tests/io/formats/test_to_html.py | 8 ++++++++ 3 files changed, 20 insertions(+) create mode 100644 pandas/tests/io/formats/data/html/gh54167_expected_output.html diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index a7a6f481ebdde..9fe8cbfa159c6 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -1960,6 +1960,8 @@ def _trim_zeros_complex(str_complexes: np.ndarray, decimal: str = ".") -> list[s # in the array n = len(str_complexes) padded_parts = _trim_zeros_float(real_part + imag_part, decimal) + if len(padded_parts) == 0: + return [] padded_length = max(len(part) for part in padded_parts) - 1 padded = [ real_pt # real part, possibly NaN diff --git a/pandas/tests/io/formats/data/html/gh54167_expected_output.html b/pandas/tests/io/formats/data/html/gh54167_expected_output.html new file mode 100644 index 0000000000000..4672c5f552b1f --- /dev/null +++ b/pandas/tests/io/formats/data/html/gh54167_expected_output.html @@ -0,0 +1,10 @@ + + + + + + + + + +
x
\ No newline at end of file diff --git a/pandas/tests/io/formats/test_to_html.py b/pandas/tests/io/formats/test_to_html.py index 13041623f4aa6..df4c279e4de86 100644 --- a/pandas/tests/io/formats/test_to_html.py +++ b/pandas/tests/io/formats/test_to_html.py @@ -959,3 +959,11 @@ def test_to_html_tuple_col_with_colspace(): "" ) assert result == expected + + +def test_to_html_empty_complex_array(datapath): + # GH#54167 + df = DataFrame({"x": np.array([], dtype="complex")}) + result = df.to_html(na_rep="-") + expected = expected_html(datapath, "gh54167_expected_output") + assert result == expected From 6f33187fc9587a87d88c2664915ebc4728409d06 Mon Sep 17 00:00:00 2001 From: Soumyadip Mal Date: Tue, 8 Aug 2023 01:01:27 +0530 Subject: [PATCH 2/9] Removed html file, put it in string in the test --- .../io/formats/data/html/gh54167_expected_output.html | 10 ---------- pandas/tests/io/formats/test_to_html.py | 5 +++-- 2 files changed, 3 insertions(+), 12 deletions(-) delete mode 100644 pandas/tests/io/formats/data/html/gh54167_expected_output.html diff --git a/pandas/tests/io/formats/data/html/gh54167_expected_output.html b/pandas/tests/io/formats/data/html/gh54167_expected_output.html deleted file mode 100644 index 4672c5f552b1f..0000000000000 --- a/pandas/tests/io/formats/data/html/gh54167_expected_output.html +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - -
x
\ No newline at end of file diff --git a/pandas/tests/io/formats/test_to_html.py b/pandas/tests/io/formats/test_to_html.py index df4c279e4de86..69dbf82063cd3 100644 --- a/pandas/tests/io/formats/test_to_html.py +++ b/pandas/tests/io/formats/test_to_html.py @@ -961,9 +961,10 @@ def test_to_html_tuple_col_with_colspace(): assert result == expected -def test_to_html_empty_complex_array(datapath): +def test_to_html_empty_complex_array(): # GH#54167 df = DataFrame({"x": np.array([], dtype="complex")}) result = df.to_html(na_rep="-") - expected = expected_html(datapath, "gh54167_expected_output") + expected = '\n \n \n \n \n \n \n \n \n
x
' + print("--assserting") assert result == expected From 2ac27a0173ae5006368ac2f1b7a778e93bc5b0d2 Mon Sep 17 00:00:00 2001 From: Soumyadip Mal Date: Tue, 8 Aug 2023 01:39:05 +0530 Subject: [PATCH 3/9] Html test against hash value --- pandas/tests/io/formats/test_to_html.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/tests/io/formats/test_to_html.py b/pandas/tests/io/formats/test_to_html.py index 69dbf82063cd3..f3f3a8b0bbf5e 100644 --- a/pandas/tests/io/formats/test_to_html.py +++ b/pandas/tests/io/formats/test_to_html.py @@ -964,7 +964,9 @@ def test_to_html_tuple_col_with_colspace(): def test_to_html_empty_complex_array(): # GH#54167 df = DataFrame({"x": np.array([], dtype="complex")}) - result = df.to_html(na_rep="-") - expected = '\n \n \n \n \n \n \n \n \n
x
' + html_op = df.to_html(col_space=100) + result = hashlib.md5(b"html_op").hexdigest() + expected = "9d81f92dbc2d9f2f4876900c09559c39" + #'\n \n \n \n \n \n \n \n \n
x
' print("--assserting") assert result == expected From 6decd8c09e75cee434d2928af5380b1d8e6b2c89 Mon Sep 17 00:00:00 2001 From: Soumyadip Mal Date: Tue, 8 Aug 2023 01:45:27 +0530 Subject: [PATCH 4/9] Removing prints --- pandas/tests/io/formats/test_to_html.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pandas/tests/io/formats/test_to_html.py b/pandas/tests/io/formats/test_to_html.py index f3f3a8b0bbf5e..b6eeefe3b2373 100644 --- a/pandas/tests/io/formats/test_to_html.py +++ b/pandas/tests/io/formats/test_to_html.py @@ -967,6 +967,4 @@ def test_to_html_empty_complex_array(): html_op = df.to_html(col_space=100) result = hashlib.md5(b"html_op").hexdigest() expected = "9d81f92dbc2d9f2f4876900c09559c39" - #'\n \n \n \n \n \n \n \n \n
x
' - print("--assserting") assert result == expected From 47b64f15504cebdef76f091123314f66c36e7504 Mon Sep 17 00:00:00 2001 From: Soumyadip Mal Date: Tue, 8 Aug 2023 01:58:25 +0530 Subject: [PATCH 5/9] Keeping inline html instead of mdlib hex --- pandas/tests/io/formats/test_to_html.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/pandas/tests/io/formats/test_to_html.py b/pandas/tests/io/formats/test_to_html.py index b6eeefe3b2373..3b5fe329c320c 100644 --- a/pandas/tests/io/formats/test_to_html.py +++ b/pandas/tests/io/formats/test_to_html.py @@ -964,7 +964,17 @@ def test_to_html_tuple_col_with_colspace(): def test_to_html_empty_complex_array(): # GH#54167 df = DataFrame({"x": np.array([], dtype="complex")}) - html_op = df.to_html(col_space=100) - result = hashlib.md5(b"html_op").hexdigest() - expected = "9d81f92dbc2d9f2f4876900c09559c39" + result = df.to_html(col_space=100) + expected = ( + '\n' + " \n" + ' \n' + ' \n' + ' \n' + " \n" + " \n" + " \n" + " \n" + "
x
" + ) assert result == expected From d5b50a0758eb4dda7cdbe8e84769c7fc87167689 Mon Sep 17 00:00:00 2001 From: Soumyadip Mal Date: Tue, 8 Aug 2023 08:25:26 +0530 Subject: [PATCH 6/9] added an entry in the io section --- doc/source/whatsnew/v2.1.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index a0c5b4523f782..bf85f95beed7a 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -734,6 +734,7 @@ I/O - Bug in :meth:`DataFrame.to_json` where :class:`DateTimeArray`/:class:`DateTimeIndex` with non nanosecond precision could not be serialized correctly (:issue:`53686`) - Bug when writing and reading empty Stata dta files where dtype information was lost (:issue:`46240`) - Bug where ``bz2`` was treated as a hard requirement (:issue:`53857`) +- Bug where dataframe to html conversion for empty array with complex dtypes returned a ValueError (:issue:`54167`) Period ^^^^^^ From 1f5ca6ab3fb8acc960c3dcf648d4985ad1f3871e Mon Sep 17 00:00:00 2001 From: SOUMYADIP MAL <32342488+soumya1729@users.noreply.github.com> Date: Tue, 8 Aug 2023 22:10:18 +0530 Subject: [PATCH 7/9] Update doc/source/whatsnew/v2.1.0.rst Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> --- doc/source/whatsnew/v2.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index bf85f95beed7a..c6883ca2125fe 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -734,7 +734,7 @@ I/O - Bug in :meth:`DataFrame.to_json` where :class:`DateTimeArray`/:class:`DateTimeIndex` with non nanosecond precision could not be serialized correctly (:issue:`53686`) - Bug when writing and reading empty Stata dta files where dtype information was lost (:issue:`46240`) - Bug where ``bz2`` was treated as a hard requirement (:issue:`53857`) -- Bug where dataframe to html conversion for empty array with complex dtypes returned a ValueError (:issue:`54167`) +- Bug in :meth:`DataFrame.to_html` where conversion for an empty :class:`DataFrame` with complex dtype raised a ``ValueError`` (:issue:`54167`) Period ^^^^^^ From fe5a1f3a36ad703ef1169ef461ed0a7472f0fd5d Mon Sep 17 00:00:00 2001 From: Soumyadip Mal Date: Tue, 8 Aug 2023 22:34:18 +0530 Subject: [PATCH 8/9] Modifying rst file --- doc/source/whatsnew/v2.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index bf85f95beed7a..c6883ca2125fe 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -734,7 +734,7 @@ I/O - Bug in :meth:`DataFrame.to_json` where :class:`DateTimeArray`/:class:`DateTimeIndex` with non nanosecond precision could not be serialized correctly (:issue:`53686`) - Bug when writing and reading empty Stata dta files where dtype information was lost (:issue:`46240`) - Bug where ``bz2`` was treated as a hard requirement (:issue:`53857`) -- Bug where dataframe to html conversion for empty array with complex dtypes returned a ValueError (:issue:`54167`) +- Bug in :meth:`DataFrame.to_html` where conversion for an empty :class:`DataFrame` with complex dtype raised a ``ValueError`` (:issue:`54167`) Period ^^^^^^ From d05ec6b52e8a741c6bc14f2deb45bd48beee8753 Mon Sep 17 00:00:00 2001 From: Soumyadip Mal Date: Tue, 8 Aug 2023 23:35:59 +0530 Subject: [PATCH 9/9] Sorted rst file after manual run of precommit on local --- doc/source/whatsnew/v2.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index c6883ca2125fe..fb016defc0c56 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -731,10 +731,10 @@ I/O - Bug in :func:`read_sql` when reading multiple timezone aware columns with the same column name (:issue:`44421`) - Bug in :func:`read_xml` stripping whitespace in string data (:issue:`53811`) - Bug in :meth:`DataFrame.to_html` where ``colspace`` was incorrectly applied in case of multi index columns (:issue:`53885`) +- Bug in :meth:`DataFrame.to_html` where conversion for an empty :class:`DataFrame` with complex dtype raised a ``ValueError`` (:issue:`54167`) - Bug in :meth:`DataFrame.to_json` where :class:`DateTimeArray`/:class:`DateTimeIndex` with non nanosecond precision could not be serialized correctly (:issue:`53686`) - Bug when writing and reading empty Stata dta files where dtype information was lost (:issue:`46240`) - Bug where ``bz2`` was treated as a hard requirement (:issue:`53857`) -- Bug in :meth:`DataFrame.to_html` where conversion for an empty :class:`DataFrame` with complex dtype raised a ``ValueError`` (:issue:`54167`) Period ^^^^^^