Skip to content

Commit f823a85

Browse files
authored
BUG: DataFrame.to_html ignores formatters (#37958)
1 parent 191d633 commit f823a85

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,7 @@ I/O
656656
- Bug in :meth:`read_fw` was not skipping blank lines (even with ``skip_blank_lines=True``) (:issue:`37758`)
657657
- Parse missing values using :func:`read_json` with ``dtype=False`` to ``NaN`` instead of ``None`` (:issue:`28501`)
658658
- :meth:`read_fwf` was inferring compression with ``compression=None`` which was not consistent with the other :meth:``read_*`` functions (:issue:`37909`)
659+
- :meth:`DataFrame.to_html` was ignoring ``formatters`` argument for ``ExtensionDtype`` columns (:issue:`36525`)
659660

660661
Period
661662
^^^^^^

pandas/io/formats/format.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1537,7 +1537,9 @@ class ExtensionArrayFormatter(GenericArrayFormatter):
15371537
def _format_strings(self) -> List[str]:
15381538
values = extract_array(self.values, extract_numpy=True)
15391539

1540-
formatter = values._formatter(boxed=True)
1540+
formatter = self.formatter
1541+
if formatter is None:
1542+
formatter = values._formatter(boxed=True)
15411543

15421544
if is_categorical_dtype(values.dtype):
15431545
# Categorical is special for now, so that we can preserve tzinfo
@@ -1553,7 +1555,9 @@ def _format_strings(self) -> List[str]:
15531555
digits=self.digits,
15541556
space=self.space,
15551557
justify=self.justify,
1558+
decimal=self.decimal,
15561559
leading_space=self.leading_space,
1560+
quoting=self.quoting,
15571561
)
15581562
return fmt_values
15591563

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<table border="1" class="dataframe">
2+
<thead>
3+
<tr style="text-align: right;">
4+
<th></th>
5+
<th>i</th>
6+
<th>f</th>
7+
<th>I</th>
8+
<th>s</th>
9+
<th>b</th>
10+
<th>c</th>
11+
<th>o</th>
12+
</tr>
13+
</thead>
14+
<tbody>
15+
<tr>
16+
<th>0</th>
17+
<td>formatted</td>
18+
<td>formatted</td>
19+
<td>formatted</td>
20+
<td>formatted</td>
21+
<td>formatted</td>
22+
<td>formatted</td>
23+
<td>formatted</td>
24+
</tr>
25+
<tr>
26+
<th>1</th>
27+
<td>formatted</td>
28+
<td>formatted</td>
29+
<td>formatted</td>
30+
<td>formatted</td>
31+
<td>formatted</td>
32+
<td>formatted</td>
33+
<td>formatted</td>
34+
</tr>
35+
</tbody>
36+
</table>

pandas/tests/io/formats/test_to_html.py

+15
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,21 @@ def test_to_html_multiindex_odd_even_truncate(max_rows, expected, datapath):
247247
{"hod": lambda x: x.strftime("%H:%M")},
248248
"datetime64_hourformatter",
249249
),
250+
(
251+
DataFrame(
252+
{
253+
"i": pd.Series([1, 2], dtype="int64"),
254+
"f": pd.Series([1, 2], dtype="float64"),
255+
"I": pd.Series([1, 2], dtype="Int64"),
256+
"s": pd.Series([1, 2], dtype="string"),
257+
"b": pd.Series([True, False], dtype="boolean"),
258+
"c": pd.Series(["a", "b"], dtype=pd.CategoricalDtype(["a", "b"])),
259+
"o": pd.Series([1, "2"], dtype=object),
260+
}
261+
),
262+
[lambda x: "formatted"] * 7,
263+
"various_dtypes_formatted",
264+
),
250265
],
251266
)
252267
def test_to_html_formatters(df, formatters, expected, datapath):

0 commit comments

Comments
 (0)