Skip to content

BUG: DataFrame.to_html ignores formatters #37958

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 6 commits into from
Nov 21, 2020
Merged
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/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ I/O
- :meth:`to_excel` and :meth:`to_markdown` support writing to fsspec URLs such as S3 and Google Cloud Storage (:issue:`33987`)
- Bug in :meth:`read_fw` was not skipping blank lines (even with ``skip_blank_lines=True``) (:issue:`37758`)
- :meth:`read_fwf` was inferring compression with ``compression=None`` which was not consistent with the other :meth:``read_*`` functions (:issue:`37909`)
- :meth:`DataFrame.to_html` was ignoring ``formatters`` argument for ``ExtensionDtype`` columns (:issue:`36525`)

Period
^^^^^^
Expand Down
6 changes: 5 additions & 1 deletion pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1537,7 +1537,9 @@ class ExtensionArrayFormatter(GenericArrayFormatter):
def _format_strings(self) -> List[str]:
values = extract_array(self.values, extract_numpy=True)

formatter = values._formatter(boxed=True)
formatter = self.formatter
if formatter is None:
formatter = values._formatter(boxed=True)

if is_categorical_dtype(values.dtype):
# Categorical is special for now, so that we can preserve tzinfo
Expand All @@ -1553,7 +1555,9 @@ def _format_strings(self) -> List[str]:
digits=self.digits,
space=self.space,
justify=self.justify,
decimal=self.decimal,
leading_space=self.leading_space,
quoting=self.quoting,
)
return fmt_values

Expand Down
36 changes: 36 additions & 0 deletions pandas/tests/io/formats/data/html/various_dtypes_formatted.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>i</th>
<th>f</th>
<th>I</th>
<th>s</th>
<th>b</th>
<th>c</th>
<th>o</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>formatted</td>
<td>formatted</td>
<td>formatted</td>
<td>formatted</td>
<td>formatted</td>
<td>formatted</td>
<td>formatted</td>
</tr>
<tr>
<th>1</th>
<td>formatted</td>
<td>formatted</td>
<td>formatted</td>
<td>formatted</td>
<td>formatted</td>
<td>formatted</td>
<td>formatted</td>
</tr>
</tbody>
</table>
15 changes: 15 additions & 0 deletions pandas/tests/io/formats/test_to_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,21 @@ def test_to_html_multiindex_odd_even_truncate(max_rows, expected, datapath):
{"hod": lambda x: x.strftime("%H:%M")},
"datetime64_hourformatter",
),
(
DataFrame(
{
"i": pd.Series([1, 2], dtype="int64"),
"f": pd.Series([1, 2], dtype="float64"),
"I": pd.Series([1, 2], dtype="Int64"),
"s": pd.Series([1, 2], dtype="string"),
"b": pd.Series([True, False], dtype="boolean"),
"c": pd.Series(["a", "b"], dtype=pd.CategoricalDtype(["a", "b"])),
"o": pd.Series([1, "2"], dtype=object),
}
),
[lambda x: "formatted"] * 7,
"various_dtypes_formatted",
),
],
)
def test_to_html_formatters(df, formatters, expected, datapath):
Expand Down