Skip to content

Commit 4be9f52

Browse files
attack68yehoshuadimarsky
authored andcommitted
REGR: Styler buf and encoding in to_latex and to_html (pandas-dev#47059)
1 parent 53c63b4 commit 4be9f52

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

doc/source/whatsnew/v1.4.3.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Fixed regressions
1616
~~~~~~~~~~~~~~~~~
1717
- Fixed regression in :meth:`DataFrame.nsmallest` led to wrong results when ``np.nan`` in the sorting column (:issue:`46589`)
1818
- Fixed regression in :func:`read_fwf` raising ``ValueError`` when ``widths`` was specified with ``usecols`` (:issue:`46580`)
19-
-
19+
- Fixed regression is :meth:`.Styler.to_latex` and :meth:`.Styler.to_html` where ``buf`` failed in combination with ``encoding`` (:issue:`47053`)
2020

2121
.. ---------------------------------------------------------------------------
2222

pandas/io/formats/style.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -1153,10 +1153,12 @@ def to_latex(
11531153
clines=clines,
11541154
)
11551155

1156-
encoding = encoding or get_option("styler.render.encoding")
1157-
return save_to_buffer(
1158-
latex, buf=buf, encoding=None if buf is None else encoding
1156+
encoding = (
1157+
(encoding or get_option("styler.render.encoding"))
1158+
if isinstance(buf, str) # i.e. a filepath
1159+
else encoding
11591160
)
1161+
return save_to_buffer(latex, buf=buf, encoding=encoding)
11601162

11611163
@Substitution(buf=buf, encoding=encoding)
11621164
def to_html(
@@ -1273,15 +1275,14 @@ def to_html(
12731275
if caption is not None:
12741276
obj.set_caption(caption)
12751277

1276-
encoding = encoding or get_option("styler.render.encoding")
12771278
# Build HTML string..
12781279
html = obj._render_html(
12791280
sparse_index=sparse_index,
12801281
sparse_columns=sparse_columns,
12811282
max_rows=max_rows,
12821283
max_cols=max_columns,
12831284
exclude_styles=exclude_styles,
1284-
encoding=encoding,
1285+
encoding=encoding or get_option("styler.render.encoding"),
12851286
doctype_html=doctype_html,
12861287
**kwargs,
12871288
)

pandas/tests/io/formats/style/test_style.py

+7
Original file line numberDiff line numberDiff line change
@@ -1557,3 +1557,10 @@ def test_no_empty_apply(mi_styler):
15571557
# 45313
15581558
mi_styler.apply(lambda s: ["a:v;"] * 2, subset=[False, False])
15591559
mi_styler._compute()
1560+
1561+
1562+
@pytest.mark.parametrize("format", ["html", "latex", "string"])
1563+
def test_output_buffer(mi_styler, format):
1564+
# gh 47053
1565+
with open(f"delete_me.{format}", "w") as f:
1566+
getattr(mi_styler, f"to_{format}")(f)

0 commit comments

Comments
 (0)