diff --git a/doc/source/whatsnew/v1.2.0.rst b/doc/source/whatsnew/v1.2.0.rst index cea42cbffa906..570c9999843c8 100644 --- a/doc/source/whatsnew/v1.2.0.rst +++ b/doc/source/whatsnew/v1.2.0.rst @@ -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 ^^^^^^ diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 8ede35912a492..082c539d034eb 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -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 @@ -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 diff --git a/pandas/tests/io/formats/data/html/various_dtypes_formatted.html b/pandas/tests/io/formats/data/html/various_dtypes_formatted.html new file mode 100644 index 0000000000000..7d2ede3379213 --- /dev/null +++ b/pandas/tests/io/formats/data/html/various_dtypes_formatted.html @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
ifIsbco
0formattedformattedformattedformattedformattedformattedformatted
1formattedformattedformattedformattedformattedformattedformatted
diff --git a/pandas/tests/io/formats/test_to_html.py b/pandas/tests/io/formats/test_to_html.py index f4f963d268aeb..aaadc965aca52 100644 --- a/pandas/tests/io/formats/test_to_html.py +++ b/pandas/tests/io/formats/test_to_html.py @@ -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):