Skip to content
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.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^^^^^^^
Expand Down
11 changes: 10 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2207,6 +2207,7 @@ def to_html(
border=None,
table_id=None,
render_links=False,
encoding=None,
):
"""
Render a DataFrame as an HTML table.
Expand All @@ -2222,6 +2223,10 @@ def to_html(
border : int
A ``border=border`` attribute is included in the opening
`<table>` 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 `<table>` tag if specified.

Expand Down Expand Up @@ -2263,7 +2268,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,
)

# ----------------------------------------------------------------------
Expand Down
5 changes: 4 additions & 1 deletion pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/io/formats/test_to_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]})
Expand Down