Skip to content

BUG: na_rep given precedence in to_html #13911

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

Closed
wants to merge 1 commit into from
Closed
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/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ Bug Fixes
- Bug in ``DataFrame`` construction in which unsigned 64-bit integer elements were being converted to objects (:issue:`14881`)
- Bug in ``astype()`` where ``inf`` values were incorrectly converted to integers. Now raises error now with ``astype()`` for Series and DataFrames (:issue:`14265`)
- Bug in ``describe()`` when passing a numpy array which does not contain the median to the ``percentiles`` keyword argument (:issue:`14908`)
- Bug in ``.to_html``, ``.to_latex`` and ``.to_string`` ignoring ``na_rep`` in the presence of a ``float_format`` function. (:issue:`13911`)



Expand Down
16 changes: 14 additions & 2 deletions pandas/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -2063,7 +2063,19 @@ def get_result_as_array(self):
"""

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

Choose a reason for hiding this comment

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

you are repeating lots of code from right below format_values_with. see if you can simplify

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 update here

fmt_values = np.array([self.formatter(x)
for x in self.values])
else:
fmt_values = self.values
mask = isnull(fmt_values)
fmt_values = np.array(fmt_values, dtype='object')
fmt_values[mask] = self.na_rep
imask = (~mask).ravel()
fmt_values.flat[imask] = np.array([self.formatter(x)
for x in
fmt_values.ravel()[imask]])
return fmt_values

if self.fixed_width:
threshold = get_option("display.chop_threshold")
Expand Down Expand Up @@ -2129,7 +2141,7 @@ def format_values_with(float_format):

def _format_strings(self):
# shortcut
if self.formatter is not None:
if self.formatter is not None and self.na_rep is None:
return [self.formatter(x) for x in self.values]

return list(self.get_result_as_array())
Expand Down
54 changes: 54 additions & 0 deletions pandas/tests/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1667,6 +1667,34 @@ def test_to_html_border_zero(self):
result = df.to_html(border=0)
self.assertTrue('border="0"' in result)

def test_to_html_na_rep_and_float_format(self):
# GH 13828
df = DataFrame([['A', 1.2225], ['A', ]], columns=['Group', 'Data'])
result = df.to_html(na_rep='Ted', float_format='{0:.2f}'.format)
expected = '''\
<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>Ted</td>
</tr>
</tbody>
</table>'''
self.assertEqual(result, expected)

def test_nonunicode_nonascii_alignment(self):
df = DataFrame([["aa\xc3\xa4\xc3\xa4", 1], ["bbbb", 2]])
rep_str = df.to_string()
Expand Down Expand Up @@ -2906,6 +2934,22 @@ def test_to_latex_with_formatters(self):
"""
self.assertEqual(result, expected)

def test_to_latex_na_rep_and_float_format(self):
# GH 13828
df = DataFrame([['A', 1.2225], ['A', ]], columns=['Group', 'Data'])
result = df.to_latex(na_rep='Ted', float_format='{0:.2f}'.format)
expected = '''\
\\begin{tabular}{llr}
\\toprule
{} & Group & Data \\\\
\\midrule
0 & A & 1.22 \\\\
1 & A & Ted \\\\
\\bottomrule
\\end{tabular}
'''
self.assertEqual(result, expected)

def test_to_latex_multiindex(self):
df = DataFrame({('x', 'y'): ['a']})
result = df.to_latex()
Expand Down Expand Up @@ -3450,6 +3494,16 @@ def test_to_string_float_na_spacing(self):
'3 -3.0000\n' + '4 NaN')
self.assertEqual(result, expected)

def test_to_string_na_rep_and_float_format(self):
# GH 13828
df = DataFrame([['A', 1.2225], ['A', ]], columns=['Group', 'Data'])
result = df.to_string(na_rep='Ted', float_format='{0:.2f}'.format)
expected = '''\
Group Data
0 A 1.22
1 A Ted'''
self.assertEqual(result, expected)

def test_to_string_without_index(self):
# GH 11729 Test index=False option
s = Series([1, 2, 3, 4])
Expand Down