From b94e50a3aab468e0b519d1a06d713d0e0988e3ed Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Wed, 18 May 2022 23:18:44 +0200 Subject: [PATCH 1/4] HTML and Latex buffer edits --- pandas/io/formats/style.py | 15 +++++++-------- pandas/tests/io/formats/style/test_style.py | 7 +++++++ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 54568d5cd18bb..f8c380836cd4d 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -1153,10 +1153,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) @Substitution(buf=buf, encoding=encoding) def to_html( @@ -1273,7 +1275,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, @@ -1281,14 +1282,12 @@ 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, ) - return save_to_buffer( - html, buf=buf, encoding=(encoding if buf is not None else None) - ) + return save_to_buffer(html, buf=buf, encoding=None if buf is None else encoding) @Substitution(buf=buf, encoding=encoding) def to_string( diff --git a/pandas/tests/io/formats/style/test_style.py b/pandas/tests/io/formats/style/test_style.py index 4615f3ff50cbd..30da2026aaf25 100644 --- a/pandas/tests/io/formats/style/test_style.py +++ b/pandas/tests/io/formats/style/test_style.py @@ -1557,3 +1557,10 @@ def test_no_empty_apply(mi_styler): # 45313 mi_styler.apply(lambda s: ["a:v;"] * 2, subset=[False, False]) mi_styler._compute() + + +@pytest.mark.parametrize("format", ["html", "latex"]) +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 c4c4fbf7e97348a79493f84fb99782e55b3927c3 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Wed, 18 May 2022 23:21:00 +0200 Subject: [PATCH 2/4] HTML and Latex buffer edits --- doc/source/whatsnew/v1.4.3.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.3.rst b/doc/source/whatsnew/v1.4.3.rst index 0c326e15d90ed..23c8ad63bf7bb 100644 --- a/doc/source/whatsnew/v1.4.3.rst +++ b/doc/source/whatsnew/v1.4.3.rst @@ -16,7 +16,7 @@ Fixed regressions ~~~~~~~~~~~~~~~~~ - Fixed regression in :meth:`DataFrame.nsmallest` led to wrong results when ``np.nan`` in the sorting column (:issue:`46589`) - Fixed regression in :func:`read_fwf` raising ``ValueError`` when ``widths`` was specified with ``usecols`` (:issue:`46580`) -- +- Fixed regression is :meth:`.Styler.to_latex` and :meth:`.Styler.to_html` where ``buf`` failed in combination with ``encoding`` (:issue:`47053`) .. --------------------------------------------------------------------------- From 8c34b2efb78f9b26aed4b24c2ec3a29cdb40db69 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Wed, 18 May 2022 23:22:45 +0200 Subject: [PATCH 3/4] HTML and Latex buffer edits --- pandas/io/formats/style.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index f8c380836cd4d..90642ff2bb3fc 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -1287,7 +1287,9 @@ def to_html( **kwargs, ) - return save_to_buffer(html, buf=buf, encoding=None if buf is None else encoding) + return save_to_buffer( + html, buf=buf, encoding=(encoding if buf is not None else None) + ) @Substitution(buf=buf, encoding=encoding) def to_string( From 2a5dd945cd68823a8c1b2bed3bb05b38d023655c Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Wed, 18 May 2022 23:25:41 +0200 Subject: [PATCH 4/4] add string for good order --- 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 30da2026aaf25..60800bc794592 100644 --- a/pandas/tests/io/formats/style/test_style.py +++ b/pandas/tests/io/formats/style/test_style.py @@ -1559,7 +1559,7 @@ def test_no_empty_apply(mi_styler): mi_styler._compute() -@pytest.mark.parametrize("format", ["html", "latex"]) +@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: