-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Don't ignore na_rep in DataFrame.to_html #36690
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
Changes from 11 commits
e1614ae
f56d893
247e79b
f33e4d0
e83b7ed
a240b49
7ee3ef2
72a812a
ca57e06
dd25388
4a599b2
dc6287a
faa8e2c
1374cdd
c81aa04
52f16fc
8c46ab7
6cb161c
b166ffc
199c560
5a054d7
4423dd7
0c49eb0
4b53c91
87c172a
265e2a8
8f0ca15
7be7c38
5a50ad0
1af22a5
37cc78c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3432,3 +3432,14 @@ def test_format_remove_leading_space_dataframe(input_array, expected): | |
# GH: 24980 | ||
df = pd.DataFrame(input_array).to_string(index=False) | ||
assert df == expected | ||
|
||
|
||
@pytest.mark.parametrize("na_rep, string", [("NaN", "nan"), ("Ted", "Ted")]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'm not sure about using "nan" when Maybe could use maybe worth considering passing
and then use The main issue with the custom formatters (applies to The other issue is that the EAs use the custom formatters. So this is not necessarily an easy issue to fix in isolation. |
||
def test_to_string_na_rep_and_float_format(na_rep, string): | ||
# GH 13828 | ||
df = DataFrame([["A", 1.2225], ["A", None]], columns=["Group", "Data"]) | ||
result = df.to_string(na_rep=na_rep, float_format="{:.2f}".format) | ||
expected = f""" Group Data | ||
0 A 1.22 | ||
1 A {string}""" | ||
assert result == expected |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -820,3 +820,38 @@ def test_html_repr_min_rows(datapath, max_rows, min_rows, expected): | |
with option_context("display.max_rows", max_rows, "display.min_rows", min_rows): | ||
result = df._repr_html_() | ||
assert result == expected | ||
|
||
|
||
@pytest.mark.parametrize("na_rep, string", [("NaN", "nan"), ("Ted", "Ted")]) | ||
def test_to_html_na_rep_and_float_format(na_rep, string): | ||
# https://github.com/pandas-dev/pandas/issues/13828 | ||
df = DataFrame( | ||
[ | ||
["A", 1.2225], | ||
["A", None], | ||
], | ||
columns=["Group", "Data"], | ||
) | ||
result = df.to_html(na_rep=na_rep, float_format="{:.2f}".format) | ||
expected = f"""<table border="1" class="dataframe"> | ||
<thead> | ||
<tr style="text-align: right;"> | ||
<th></th> | ||
<th>Group</th> | ||
<th>Data</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<th>0</th> | ||
<td>A</td> | ||
<td>1.22</td> | ||
</tr> | ||
<tr> | ||
<th>1</th> | ||
<td>A</td> | ||
<td>{string}</td> | ||
</tr> | ||
</tbody> | ||
</table>""" | ||
assert result == expected | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for html tests, can use expected_html fixture. see test_to_html_justify for usage as template. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually this entire section you added should be instead on L1440
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and you will want to do something like
you will want to factor that out into a function and use it above in 2 (or more places)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand,
get_result_as_array
never actually gets called here. Are you thinking the formatter should already be handling NaN?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no my point is to share code; you are doing virtually the same thing, just in another way
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok I see what you mean