Skip to content

Commit f22bb8c

Browse files
soumya1729mroeschke
andcommitted
BUG: Fixes pandas-dev#54617 Dataframe to html for empty array with complex dtypes (pandas-dev#54451)
* Fresh new feature * Removed html file, put it in string in the test * Html test against hash value * Removing prints * Keeping inline html instead of mdlib hex * added an entry in the io section * Update doc/source/whatsnew/v2.1.0.rst Co-authored-by: Matthew Roeschke <[email protected]> * Modifying rst file * Sorted rst file after manual run of precommit on local --------- Co-authored-by: Matthew Roeschke <[email protected]>
1 parent e7976a7 commit f22bb8c

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,7 @@ I/O
731731
- Bug in :func:`read_sql` when reading multiple timezone aware columns with the same column name (:issue:`44421`)
732732
- Bug in :func:`read_xml` stripping whitespace in string data (:issue:`53811`)
733733
- Bug in :meth:`DataFrame.to_html` where ``colspace`` was incorrectly applied in case of multi index columns (:issue:`53885`)
734+
- Bug in :meth:`DataFrame.to_html` where conversion for an empty :class:`DataFrame` with complex dtype raised a ``ValueError`` (:issue:`54167`)
734735
- Bug in :meth:`DataFrame.to_json` where :class:`DateTimeArray`/:class:`DateTimeIndex` with non nanosecond precision could not be serialized correctly (:issue:`53686`)
735736
- Bug when writing and reading empty Stata dta files where dtype information was lost (:issue:`46240`)
736737
- Bug where ``bz2`` was treated as a hard requirement (:issue:`53857`)

pandas/io/formats/format.py

+2
Original file line numberDiff line numberDiff line change
@@ -1960,6 +1960,8 @@ def _trim_zeros_complex(str_complexes: np.ndarray, decimal: str = ".") -> list[s
19601960
# in the array
19611961
n = len(str_complexes)
19621962
padded_parts = _trim_zeros_float(real_part + imag_part, decimal)
1963+
if len(padded_parts) == 0:
1964+
return []
19631965
padded_length = max(len(part) for part in padded_parts) - 1
19641966
padded = [
19651967
real_pt # real part, possibly NaN

pandas/tests/io/formats/test_to_html.py

+19
Original file line numberDiff line numberDiff line change
@@ -959,3 +959,22 @@ def test_to_html_tuple_col_with_colspace():
959959
"</table>"
960960
)
961961
assert result == expected
962+
963+
964+
def test_to_html_empty_complex_array():
965+
# GH#54167
966+
df = DataFrame({"x": np.array([], dtype="complex")})
967+
result = df.to_html(col_space=100)
968+
expected = (
969+
'<table border="1" class="dataframe">\n'
970+
" <thead>\n"
971+
' <tr style="text-align: right;">\n'
972+
' <th style="min-width: 100px;"></th>\n'
973+
' <th style="min-width: 100px;">x</th>\n'
974+
" </tr>\n"
975+
" </thead>\n"
976+
" <tbody>\n"
977+
" </tbody>\n"
978+
"</table>"
979+
)
980+
assert result == expected

0 commit comments

Comments
 (0)