Skip to content

Commit 79a5f7c

Browse files
Jinyang Zhoujreback
Jinyang Zhou
authored andcommitted
To html encoding add (#28692)
1 parent b632ca0 commit 79a5f7c

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ Other enhancements
109109
(:issue:`28368`)
110110
- :meth:`DataFrame.to_json` now accepts an ``indent`` integer argument to enable pretty printing of JSON output (:issue:`12004`)
111111
- :meth:`read_stata` can read Stata 119 dta files. (:issue:`28250`)
112+
- Added ``encoding`` argument to :func:`DataFrame.to_html` for non-ascii text (:issue:`28663`)
112113

113114
Build Changes
114115
^^^^^^^^^^^^^

pandas/core/frame.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -2218,6 +2218,7 @@ def to_html(
22182218
border=None,
22192219
table_id=None,
22202220
render_links=False,
2221+
encoding=None,
22212222
):
22222223
"""
22232224
Render a DataFrame as an HTML table.
@@ -2233,6 +2234,10 @@ def to_html(
22332234
border : int
22342235
A ``border=border`` attribute is included in the opening
22352236
`<table>` tag. Default ``pd.options.display.html.border``.
2237+
encoding : str, default "utf-8"
2238+
Set character encoding
2239+
2240+
.. versionadded:: 1.0
22362241
table_id : str, optional
22372242
A css id is included in the opening `<table>` tag if specified.
22382243
@@ -2274,7 +2279,11 @@ def to_html(
22742279
)
22752280
# TODO: a generic formatter wld b in DataFrameFormatter
22762281
return formatter.to_html(
2277-
buf=buf, classes=classes, notebook=notebook, border=border
2282+
buf=buf,
2283+
classes=classes,
2284+
notebook=notebook,
2285+
border=border,
2286+
encoding=encoding,
22782287
)
22792288

22802289
# ----------------------------------------------------------------------

pandas/io/formats/format.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,7 @@ def _format_col(self, i: int) -> List[str]:
942942
def to_html(
943943
self,
944944
buf: Optional[FilePathOrBuffer[str]] = None,
945+
encoding: Optional[str] = None,
945946
classes: Optional[Union[str, List, Tuple]] = None,
946947
notebook: bool = False,
947948
border: Optional[int] = None,
@@ -963,7 +964,9 @@ def to_html(
963964
from pandas.io.formats.html import HTMLFormatter, NotebookFormatter
964965

965966
Klass = NotebookFormatter if notebook else HTMLFormatter
966-
return Klass(self, classes=classes, border=border).get_result(buf=buf)
967+
return Klass(self, classes=classes, border=border).get_result(
968+
buf=buf, encoding=encoding
969+
)
967970

968971
def _get_formatted_column_labels(self, frame: "DataFrame") -> List[List[str]]:
969972
from pandas.core.index import _sparsify

pandas/tests/io/formats/test_to_html.py

+8
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ def test_to_html_unicode(df, expected, datapath):
9999
assert result == expected
100100

101101

102+
def test_to_html_encoding(float_frame, tmp_path):
103+
# GH 28663
104+
path = tmp_path / "test.html"
105+
float_frame.to_html(path, encoding="gbk")
106+
with open(str(path), "r", encoding="gbk") as f:
107+
assert float_frame.to_html() == f.read()
108+
109+
102110
def test_to_html_decimal(datapath):
103111
# GH 12031
104112
df = DataFrame({"A": [6.0, 3.1, 2.2]})

0 commit comments

Comments
 (0)