diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst
index 9ce3b5be9624f5..5b5098f7d2426b 100644
--- a/doc/source/whatsnew/v1.0.0.rst
+++ b/doc/source/whatsnew/v1.0.0.rst
@@ -109,6 +109,7 @@ Other enhancements
(:issue:`28368`)
- :meth:`DataFrame.to_json` now accepts an ``indent`` integer argument to enable pretty printing of JSON output (:issue:`12004`)
- :meth:`read_stata` can read Stata 119 dta files. (:issue:`28250`)
+- Added ``encoding`` argument to :func:`DataFrame.to_html` for non-ascii text (:issue:`28663`)
Build Changes
^^^^^^^^^^^^^
diff --git a/pandas/core/frame.py b/pandas/core/frame.py
index c82d8a25fedba4..64755b2390eaf0 100644
--- a/pandas/core/frame.py
+++ b/pandas/core/frame.py
@@ -2218,6 +2218,7 @@ def to_html(
border=None,
table_id=None,
render_links=False,
+ encoding=None,
):
"""
Render a DataFrame as an HTML table.
@@ -2233,6 +2234,10 @@ def to_html(
border : int
A ``border=border`` attribute is included in the opening
`
` tag. Default ``pd.options.display.html.border``.
+ encoding : str, default "utf-8"
+ Set character encoding
+
+ .. versionadded:: 1.0
table_id : str, optional
A css id is included in the opening `` tag if specified.
@@ -2274,7 +2279,11 @@ def to_html(
)
# TODO: a generic formatter wld b in DataFrameFormatter
return formatter.to_html(
- buf=buf, classes=classes, notebook=notebook, border=border
+ buf=buf,
+ classes=classes,
+ notebook=notebook,
+ border=border,
+ encoding=encoding,
)
# ----------------------------------------------------------------------
diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py
index ad62c56a337b61..b8c40e3f62221d 100644
--- a/pandas/io/formats/format.py
+++ b/pandas/io/formats/format.py
@@ -942,6 +942,7 @@ def _format_col(self, i: int) -> List[str]:
def to_html(
self,
buf: Optional[FilePathOrBuffer[str]] = None,
+ encoding: Optional[str] = None,
classes: Optional[Union[str, List, Tuple]] = None,
notebook: bool = False,
border: Optional[int] = None,
@@ -963,7 +964,9 @@ def to_html(
from pandas.io.formats.html import HTMLFormatter, NotebookFormatter
Klass = NotebookFormatter if notebook else HTMLFormatter
- return Klass(self, classes=classes, border=border).get_result(buf=buf)
+ return Klass(self, classes=classes, border=border).get_result(
+ buf=buf, encoding=encoding
+ )
def _get_formatted_column_labels(self, frame: "DataFrame") -> List[List[str]]:
from pandas.core.index import _sparsify
diff --git a/pandas/tests/io/formats/test_to_html.py b/pandas/tests/io/formats/test_to_html.py
index ef19319e208d91..6c4a226b7ebd24 100644
--- a/pandas/tests/io/formats/test_to_html.py
+++ b/pandas/tests/io/formats/test_to_html.py
@@ -99,6 +99,14 @@ def test_to_html_unicode(df, expected, datapath):
assert result == expected
+def test_to_html_encoding(float_frame, tmp_path):
+ # GH 28663
+ path = tmp_path / "test.html"
+ float_frame.to_html(path, encoding="gbk")
+ with open(str(path), "r", encoding="gbk") as f:
+ assert float_frame.to_html() == f.read()
+
+
def test_to_html_decimal(datapath):
# GH 12031
df = DataFrame({"A": [6.0, 3.1, 2.2]})