From d61439779438bac74a29feb1d42bdde6dc6a7b1a Mon Sep 17 00:00:00 2001 From: JHM Darbyshire <24256554+attack68@users.noreply.github.com> Date: Thu, 19 May 2022 14:00:28 +0200 Subject: [PATCH 1/2] Backport PR #47059: REGR: Styler `buf` and `encoding` in `to_latex` and `to_html` --- pandas/io/formats/style.py | 11 ++++++----- pandas/tests/io/formats/style/test_style.py | 7 +++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 10cf8541effd6..4a8169c0609fd 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -1049,10 +1049,12 @@ def to_latex( clines=clines, ) - encoding = encoding or get_option("styler.render.encoding") - return save_to_buffer( - latex, buf=buf, encoding=None if buf is None else encoding + encoding = ( + (encoding or get_option("styler.render.encoding")) + if isinstance(buf, str) # i.e. a filepath + else encoding ) + return save_to_buffer(latex, buf=buf, encoding=encoding) def to_html( self, @@ -1173,7 +1175,6 @@ def to_html( if caption is not None: obj.set_caption(caption) - encoding = encoding or get_option("styler.render.encoding") # Build HTML string.. html = obj._render_html( sparse_index=sparse_index, @@ -1181,7 +1182,7 @@ def to_html( max_rows=max_rows, max_cols=max_columns, exclude_styles=exclude_styles, - encoding=encoding, + encoding=encoding or get_option("styler.render.encoding"), doctype_html=doctype_html, **kwargs, ) diff --git a/pandas/tests/io/formats/style/test_style.py b/pandas/tests/io/formats/style/test_style.py index fa054ff7ca6c0..370fae809375f 100644 --- a/pandas/tests/io/formats/style/test_style.py +++ b/pandas/tests/io/formats/style/test_style.py @@ -1547,3 +1547,10 @@ def test_col_trimming_hide_columns(): assert ctx["head"][0][c + 2]["is_visible"] == vals[1] assert len(ctx["body"][0]) == 6 # index + 2 hidden + 2 visible + trimming col + + +@pytest.mark.parametrize("format", ["html", "latex", "string"]) +def test_output_buffer(mi_styler, format): + # gh 47053 + with open(f"delete_me.{format}", "w") as f: + getattr(mi_styler, f"to_{format}")(f) From 447fb953e8dcf05f37fade9d7f1f8c1a409d72fa Mon Sep 17 00:00:00 2001 From: JHM Darbyshire <24256554+attack68@users.noreply.github.com> Date: Fri, 20 May 2022 19:33:48 +0200 Subject: [PATCH 2/2] remove string since method in 1.5 --- pandas/tests/io/formats/style/test_style.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/io/formats/style/test_style.py b/pandas/tests/io/formats/style/test_style.py index 370fae809375f..1a056f8cb3363 100644 --- a/pandas/tests/io/formats/style/test_style.py +++ b/pandas/tests/io/formats/style/test_style.py @@ -1549,7 +1549,7 @@ def test_col_trimming_hide_columns(): assert len(ctx["body"][0]) == 6 # index + 2 hidden + 2 visible + trimming col -@pytest.mark.parametrize("format", ["html", "latex", "string"]) +@pytest.mark.parametrize("format", ["html", "latex"]) def test_output_buffer(mi_styler, format): # gh 47053 with open(f"delete_me.{format}", "w") as f: