Skip to content

Commit 95a087d

Browse files
authored
BUG: DataFrame.to_html(na_rep="-") with non-scalar data (#51412)
* BUG: DataFrame.to_html(na_rep=-) with non-scalar data * fixes * fix test
1 parent f15e31f commit 95a087d

File tree

5 files changed

+55
-1
lines changed

5 files changed

+55
-1
lines changed

doc/source/whatsnew/v2.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,6 +1327,7 @@ I/O
13271327
- Bug in :func:`read_csv` unnecessarily overflowing for extension array dtype when containing ``NA`` (:issue:`32134`)
13281328
- Bug in :meth:`DataFrame.to_dict` not converting ``NA`` to ``None`` (:issue:`50795`)
13291329
- Bug in :meth:`DataFrame.to_json` where it would segfault when failing to encode a string (:issue:`50307`)
1330+
- Bug in :meth:`DataFrame.to_html` with ``na_rep`` set when the :class:`DataFrame` contains non-scalar data (:issue:`47103`)
13301331
- Bug in :func:`read_xml` where file-like objects failed when iterparse is used (:issue:`50641`)
13311332
- Bug in :func:`read_xml` ignored repeated elements when iterparse is used (:issue:`51183`)
13321333

pandas/io/formats/style_render.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1814,7 +1814,7 @@ def _maybe_wrap_formatter(
18141814
if na_rep is None:
18151815
return func_3
18161816
else:
1817-
return lambda x: na_rep if isna(x) else func_3(x)
1817+
return lambda x: na_rep if (isna(x) is True) else func_3(x)
18181818

18191819

18201820
def non_reducing_slice(slice_: Subset):
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<table border="1" class="dataframe">
2+
<thead>
3+
<tr style="text-align: right;">
4+
<th></th>
5+
<th>a</th>
6+
<th>b</th>
7+
</tr>
8+
</thead>
9+
<tbody>
10+
<tr>
11+
<th>0</th>
12+
<td>1</td>
13+
<td>[1, 2, 3]</td>
14+
</tr>
15+
</tbody>
16+
</table>

pandas/tests/io/formats/style/test_html.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,3 +976,32 @@ def html_lines(foot_prefix: str):
976976
)
977977
)
978978
assert expected_css + expected_table == result
979+
980+
981+
def test_to_html_na_rep_non_scalar_data(datapath):
982+
# GH47103
983+
df = DataFrame([dict(a=1, b=[1, 2, 3], c=np.nan)])
984+
result = df.style.format(na_rep="-").to_html(table_uuid="test")
985+
expected = """\
986+
<style type="text/css">
987+
</style>
988+
<table id="T_test">
989+
<thead>
990+
<tr>
991+
<th class="blank level0" >&nbsp;</th>
992+
<th id="T_test_level0_col0" class="col_heading level0 col0" >a</th>
993+
<th id="T_test_level0_col1" class="col_heading level0 col1" >b</th>
994+
<th id="T_test_level0_col2" class="col_heading level0 col2" >c</th>
995+
</tr>
996+
</thead>
997+
<tbody>
998+
<tr>
999+
<th id="T_test_level0_row0" class="row_heading level0 row0" >0</th>
1000+
<td id="T_test_row0_col0" class="data row0 col0" >1</td>
1001+
<td id="T_test_row0_col1" class="data row0 col1" >[1, 2, 3]</td>
1002+
<td id="T_test_row0_col2" class="data row0 col2" >-</td>
1003+
</tr>
1004+
</tbody>
1005+
</table>
1006+
"""
1007+
assert result == expected

pandas/tests/io/formats/test_to_html.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,14 @@ def test_to_html_na_rep_and_float_format(na_rep, datapath):
882882
assert result == expected
883883

884884

885+
def test_to_html_na_rep_non_scalar_data(datapath):
886+
# GH47103
887+
df = DataFrame([dict(a=1, b=[1, 2, 3])])
888+
result = df.to_html(na_rep="-")
889+
expected = expected_html(datapath, "gh47103_expected_output")
890+
assert result == expected
891+
892+
885893
def test_to_html_float_format_object_col(datapath):
886894
# GH#40024
887895
df = DataFrame(data={"x": [1000.0, "test"]})

0 commit comments

Comments
 (0)