Skip to content

BUG: DataFrame.to_html(na_rep="-") with non-scalar data #51412

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 3 commits into from
Feb 16, 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.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)

Expand Down
2 changes: 1 addition & 1 deletion pandas/io/formats/style_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/io/formats/data/html/gh47103_expected_output.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>a</th>
<th>b</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>1</td>
<td>[1, 2, 3]</td>
</tr>
</tbody>
</table>
29 changes: 29 additions & 0 deletions pandas/tests/io/formats/style/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """\
<style type="text/css">
</style>
<table id="T_test">
<thead>
<tr>
<th class="blank level0" >&nbsp;</th>
<th id="T_test_level0_col0" class="col_heading level0 col0" >a</th>
<th id="T_test_level0_col1" class="col_heading level0 col1" >b</th>
<th id="T_test_level0_col2" class="col_heading level0 col2" >c</th>
</tr>
</thead>
<tbody>
<tr>
<th id="T_test_level0_row0" class="row_heading level0 row0" >0</th>
<td id="T_test_row0_col0" class="data row0 col0" >1</td>
<td id="T_test_row0_col1" class="data row0 col1" >[1, 2, 3]</td>
<td id="T_test_row0_col2" class="data row0 col2" >-</td>
</tr>
</tbody>
</table>
"""
assert result == expected
8 changes: 8 additions & 0 deletions pandas/tests/io/formats/test_to_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = DataFrame([dict(a=1, b=[1, 2, 3])])
result = df.to_html(na_rep="-")
expected = expected_html(datapath, "gh47103_expected_output")
assert result == expected


def test_to_html_float_format_object_col(datapath):
# GH#40024
df = DataFrame(data={"x": [1000.0, "test"]})
Expand Down