Skip to content

to_html formatter not called for float values in a mixed-type column (2) #26000

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
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
71e8b31
to_html formatter not called for float values in a mixed-type column
simonjayhawkins Apr 3, 2019
2a2bb57
changes to test as requested
simonjayhawkins Apr 4, 2019
1e5615b
Merge remote-tracking branch 'upstream/master' into GenericArrayForma…
simonjayhawkins Apr 4, 2019
4ca48a1
Merge branch 'master' into GenericArrayFormatter
simonjayhawkins Apr 4, 2019
4ef3149
Merge remote-tracking branch 'upstream/master' into GenericArrayForma…
simonjayhawkins Apr 5, 2019
d7a8510
Merge remote-tracking branch 'upstream/master' into GenericArrayForma…
simonjayhawkins Apr 5, 2019
4b60e4b
shortcut format_array
simonjayhawkins Apr 5, 2019
5ac441b
Merge remote-tracking branch 'upstream/master' into GenericArrayForma…
simonjayhawkins Apr 5, 2019
8a64459
add shortcut parameter to format_array
simonjayhawkins Apr 5, 2019
9c1354c
add whatsnew for #26002
simonjayhawkins Apr 5, 2019
d0df1d6
remove shortcut parameter from format_array
simonjayhawkins Apr 6, 2019
c74b0aa
Merge remote-tracking branch 'upstream/master' into GenericArrayForma…
simonjayhawkins Apr 6, 2019
4262113
remove whatsnew for #26002
simonjayhawkins Apr 6, 2019
f0cf9b7
defer to GenericArrayFormatter for IntervalArray
simonjayhawkins Apr 7, 2019
d6bee41
Merge remote-tracking branch 'upstream/master' into GenericArrayForma…
simonjayhawkins Jun 13, 2019
1c535a1
Merge remote-tracking branch 'upstream/master' into GenericArrayForma…
simonjayhawkins Jun 18, 2019
5ecf91a
pre-format instead of shortcut
simonjayhawkins Jun 18, 2019
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 @@ -347,6 +347,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
4 changes: 4 additions & 0 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,10 @@ def value_counts(self, dropna=True):

# Formatting

def _formatter(self, boxed=False):
# Defer to GenericArrayFormatter's formatter.
return None

def _format_data(self):

# TODO: integrate with categorical and make generic
Expand Down
4 changes: 4 additions & 0 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,10 @@ def get_result(self):
return _make_fixed_width(fmt_values, self.justify)

def _format_strings(self):
# shortcut
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 give some more explanation here

if self.formatter is not None:
return [' {}'.format(self.formatter(x)) for x in self.values]

if self.float_format is None:
float_format = get_option("display.float_format")
if float_format is None:
Expand Down
26 changes: 26 additions & 0 deletions pandas/tests/io/formats/data/html/gh13021_expected_output.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>x</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>a</td>
</tr>
<tr>
<th>1</th>
<td>$0</td>
</tr>
<tr>
<th>2</th>
<td>$10</td>
</tr>
<tr>
<th>3</th>
<td>$3</td>
</tr>
</tbody>
</table>
11 changes: 11 additions & 0 deletions pandas/tests/io/formats/test_to_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,17 @@ def test_to_html_invalid_classes_type(classes):
df.to_html(classes=classes)


def test_to_html_formatters_object_type(datapath):
# GH 13021
def f(x):
return x if isinstance(x, str) else '${:,.0f}'.format(x)

df = pd.DataFrame([['a'], [0], [10.4], [3]], columns=['x'])
result = df.to_html(formatters=dict(x=f))
expected = expected_html(datapath, 'gh13021_expected_output')
assert result == expected


def test_to_html_round_column_headers():
# GH 17280
df = DataFrame([1], columns=[0.55555])
Expand Down