Skip to content

to_html formatter not called for float values in a mixed-type column #25983

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 5 commits into from
Apr 5, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ I/O
^^^

- Bug in :func:`DataFrame.to_html()` where values were truncated using display options instead of outputting the full content (:issue:`17004`)
- Bug in :meth:`DataFrame.to_html` that would ignore ``formatters`` argument for float values in a column with ``dtype=object`` (:issue:`13021`)
- Fixed bug in missing text when using :meth:`to_clipboard` if copying utf-16 characters in Python 3 on Windows (:issue:`25040`)
- Bug in :func:`read_json` for ``orient='table'`` when it tries to infer dtypes by default, which is not applicable as dtypes are already defined in the JSON schema (:issue:`21345`)
- Bug in :func:`read_json` for ``orient='table'`` and float index, as it infers index dtype by default, which is not applicable because index dtype is already defined in the JSON schema (:issue:`25433`)
Expand Down
17 changes: 11 additions & 6 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -980,19 +980,24 @@ def _format(x):
if leading_space is None:
leading_space = is_float_type.any()

if leading_space is False:
# False specifically, so that the default is
# to include a space if we get here.
tpl = '{v}'
else:
tpl = ' {v}'

# shortcut
if self.formatter is not None:
return [tpl.format(v=self.formatter(x)) for x in self.values]

fmt_values = []
for i, v in enumerate(vals):
if not is_float_type[i] and leading_space:
fmt_values.append(' {v}'.format(v=_format(v)))
elif is_float_type[i]:
fmt_values.append(float_format(v))
else:
if leading_space is False:
# False specifically, so that the default is
# to include a space if we get here.
tpl = '{v}'
else:
tpl = ' {v}'
fmt_values.append(tpl.format(v=_format(v)))

return fmt_values
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/io/formats/test_to_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,3 +633,13 @@ def test_to_html_invalid_classes_type(classes):

with pytest.raises(TypeError, match=msg):
df.to_html(classes=classes)


def test_to_html_formatters_object_type():
# GH 13021
def f(x):
return x if type(x) is str else '${:,.0f}'.format(x)
Copy link
Member

Choose a reason for hiding this comment

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

Use isinstance instead of type


df = pd.DataFrame([['a'], [0], [10.4], [3]], columns=['x'])
result = df.to_html(formatters=dict(x=f))
assert '$10' in result
Copy link
Member

Choose a reason for hiding this comment

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

Can we make a better assertion here? I understand the original issue pertained to floats only but probably doesn't hurt to make an assertion around the other values here as well