Skip to content

BUG: Fixes #54617 Dataframe to html for empty array with complex dtypes #54451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,7 @@ 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`)
Expand Down
2 changes: 2 additions & 0 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/io/formats/test_to_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -959,3 +959,22 @@ def test_to_html_tuple_col_with_colspace():
"</table>"
)
assert result == expected


def test_to_html_empty_complex_array():
# GH#54167
df = DataFrame({"x": np.array([], dtype="complex")})
result = df.to_html(col_space=100)
expected = (
'<table border="1" class="dataframe">\n'
" <thead>\n"
' <tr style="text-align: right;">\n'
' <th style="min-width: 100px;"></th>\n'
' <th style="min-width: 100px;">x</th>\n'
" </tr>\n"
" </thead>\n"
" <tbody>\n"
" </tbody>\n"
"</table>"
)
assert result == expected