Skip to content

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

Merged
merged 31 commits into from
Oct 24, 2020
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
e1614ae
BUG: Don't ignore na_rep in DataFrame.to_html
dsaxton Sep 27, 2020
f56d893
Back to list
dsaxton Sep 27, 2020
247e79b
Merge remote-tracking branch 'upstream/master' into na_rep-with-float…
dsaxton Sep 28, 2020
f33e4d0
Merge remote-tracking branch 'upstream/master' into na_rep-with-float…
dsaxton Sep 29, 2020
e83b7ed
Merge remote-tracking branch 'upstream/master' into na_rep-with-float…
dsaxton Sep 29, 2020
a240b49
Move test and cover
dsaxton Sep 29, 2020
7ee3ef2
Test for to_latex
dsaxton Sep 29, 2020
72a812a
More tests
dsaxton Sep 29, 2020
ca57e06
Maybe
dsaxton Sep 30, 2020
dd25388
Merge remote-tracking branch 'upstream/master' into na_rep-with-float…
dsaxton Sep 30, 2020
4a599b2
Merge remote-tracking branch 'upstream/master' into na_rep-with-float…
dsaxton Sep 30, 2020
dc6287a
Merge remote-tracking branch 'upstream/master' into na_rep-with-float…
dsaxton Oct 2, 2020
faa8e2c
Note
dsaxton Oct 2, 2020
1374cdd
Merge remote-tracking branch 'upstream/master' into na_rep-with-float…
dsaxton Oct 13, 2020
c81aa04
Refactor
dsaxton Oct 13, 2020
52f16fc
Move note
dsaxton Oct 13, 2020
8c46ab7
Nothing
dsaxton Oct 13, 2020
6cb161c
Fixup
dsaxton Oct 13, 2020
b166ffc
Remove
dsaxton Oct 13, 2020
199c560
Merge remote-tracking branch 'upstream/master' into na_rep-with-float…
dsaxton Oct 14, 2020
5a054d7
Merge remote-tracking branch 'upstream/master' into na_rep-with-float…
dsaxton Oct 14, 2020
4423dd7
Merge remote-tracking branch 'upstream/master' into na_rep-with-float…
dsaxton Oct 17, 2020
0c49eb0
Merge remote-tracking branch 'upstream/master' into na_rep-with-float…
dsaxton Oct 18, 2020
4b53c91
Doc
dsaxton Oct 18, 2020
87c172a
Merge remote-tracking branch 'upstream/master' into na_rep-with-float…
dsaxton Oct 20, 2020
265e2a8
Merge remote-tracking branch 'upstream/master' into na_rep-with-float…
dsaxton Oct 20, 2020
8f0ca15
Type
dsaxton Oct 20, 2020
7be7c38
Merge remote-tracking branch 'upstream/master' into na_rep-with-float…
dsaxton Oct 21, 2020
5a50ad0
Merge remote-tracking branch 'upstream/master' into na_rep-with-float…
dsaxton Oct 21, 2020
1af22a5
Merge remote-tracking branch 'upstream/master' into na_rep-with-float…
dsaxton Oct 23, 2020
37cc78c
Merge remote-tracking branch 'upstream/master' into na_rep-with-float…
dsaxton Oct 23, 2020
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,6 @@ Strings
^^^^^^^
- Bug in :meth:`Series.to_string`, :meth:`DataFrame.to_string`, and :meth:`DataFrame.to_latex` adding a leading space when ``index=False`` (:issue:`24980`)
-
-


Interval
Expand Down Expand Up @@ -462,6 +461,7 @@ I/O
- Bug in :func:`read_table` and :func:`read_csv` when ``delim_whitespace=True`` and ``sep=default`` (:issue:`36583`)
- Bug in :meth:`to_json` with ``lines=True`` and ``orient='records'`` the last line of the record is not appended with 'new line character' (:issue:`36888`)
- Bug in :meth:`read_parquet` with fixed offset timezones. String representation of timezones was not recognized (:issue:`35997`, :issue:`36004`)
- Bug in :meth:`DataFrame.to_html`, :meth:`DataFrame.to_string`, and :meth:`DataFrame.to_latex` ignoring the ``na_rep`` argument when ``float_format`` was also specified (:issue:`9046`, :issue:`13828`)
- Bug in output rendering of complex numbers showing too many trailing zeros (:issue:`36799`)
- Bug in :class:`HDFStore` threw a ``TypeError`` when exporting an empty :class:`DataFrame` with ``datetime64[ns, tz]`` dtypes with a fixed HDF5 store (:issue:`20594`)

Expand Down
34 changes: 20 additions & 14 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,20 @@
index : bool, optional, default True
Whether to print index (row) labels.
na_rep : str, optional, default 'NaN'
String representation of NAN to use.
String representation of ``NaN`` to use.
formatters : list, tuple or dict of one-param. functions, optional
Formatter functions to apply to columns' elements by position or
name.
The result of each function must be a unicode string.
List/tuple must be of length equal to the number of columns.
float_format : one-parameter function, optional, default None
Formatter function to apply to columns' elements if they are
floats. The result of this function must be a unicode string.
floats. This function must return a unicode string and will be
applied only to the non-``NaN`` elements, with ``NaN`` being
handled by ``na_rep``.

.. versionchanged:: 1.2.0

sparsify : bool, optional, default True
Set to False for a DataFrame with a hierarchical index to print
every multiindex key at each row.
Expand Down Expand Up @@ -1444,8 +1449,19 @@ def get_result_as_array(self) -> np.ndarray:
Returns the float values converted into strings using
the parameters given at initialisation, as a numpy array
"""

def format_with_na_rep(values, formatter, na_rep):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you type

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some types

mask = isna(values)
formatted = np.array(
[
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you use this function more generally? (e.g. maybe define it in the module); can be a followup as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems similar patterns occur elsewhere, although most are at the scalar level

formatter(val) if not m else na_rep
for val, m in zip(values.ravel(), mask.ravel())
]
).reshape(values.shape)
return formatted

if self.formatter is not None:
return np.array([self.formatter(x) for x in self.values])
return format_with_na_rep(self.values, self.formatter, self.na_rep)

if self.fixed_width:
threshold = get_option("display.chop_threshold")
Expand All @@ -1466,13 +1482,7 @@ def format_values_with(float_format):
# separate the wheat from the chaff
values = self.values
is_complex = is_complex_dtype(values)
mask = isna(values)
values = np.array(values, dtype="object")
values[mask] = na_rep
imask = (~mask).ravel()
values.flat[imask] = np.array(
[formatter(val) for val in values.ravel()[imask]]
)
values = format_with_na_rep(values, formatter, na_rep)

if self.fixed_width:
if is_complex:
Expand Down Expand Up @@ -1534,10 +1544,6 @@ def format_values_with(float_format):
return formatted_values

def _format_strings(self) -> List[str]:
# shortcut
if self.formatter is not None:
return [self.formatter(x) for x in self.values]

return list(self.get_result_as_array())


Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -3434,6 +3434,17 @@ def test_format_remove_leading_space_dataframe(input_array, expected):
assert df == expected


@pytest.mark.parametrize("na_rep", ["NaN", "Ted"])
def test_to_string_na_rep_and_float_format(na_rep):
# 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 {na_rep}"""
assert result == expected


def test_to_string_complex_number_trims_zeros():
s = pd.Series([1.000000 + 1.000000j, 1.0 + 1.0j, 1.05 + 1.0j])
result = s.to_string()
Expand Down
35 changes: 35 additions & 0 deletions pandas/tests/io/formats/test_to_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", ["NaN", "Ted"])
def test_to_html_na_rep_and_float_format(na_rep):
# 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>{na_rep}</td>
</tr>
</tbody>
</table>"""
assert result == expected
Copy link
Member

Choose a reason for hiding this comment

The 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.

21 changes: 21 additions & 0 deletions pandas/tests/io/formats/test_to_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -1431,3 +1431,24 @@ def test_get_strrow_multindex_multicolumn(self, row_num, expected):
)

assert row_string_converter.get_strrow(row_num=row_num) == expected

@pytest.mark.parametrize("na_rep", ["NaN", "Ted"])
def test_to_latex_na_rep_and_float_format(self, na_rep):
df = DataFrame(
[
["A", 1.2225],
["A", None],
],
columns=["Group", "Data"],
)
result = df.to_latex(na_rep=na_rep, float_format="{:.2f}".format)
expected = f"""\\begin{{tabular}}{{llr}}
\\toprule
{{}} & Group & Data \\\\
\\midrule
0 & A & 1.22 \\\\
1 & A & {na_rep} \\\\
\\bottomrule
\\end{{tabular}}
"""
assert result == expected