From 93cf31245946864f409470a5cce5e2d467900825 Mon Sep 17 00:00:00 2001 From: Tom Aarsen Date: Thu, 2 Jun 2022 20:13:07 +0000 Subject: [PATCH 1/5] BUG: Tackle GH47203; Ensure body is non-empty when accessing row_body_cells --- pandas/io/formats/style_render.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py index 4e3f86d21b228..65720e675a77a 100644 --- a/pandas/io/formats/style_render.py +++ b/pandas/io/formats/style_render.py @@ -908,7 +908,7 @@ def concatenated_visible_rows(obj, n, row_indices): f"of 'all;data', 'all;index', 'skip-last;data', 'skip-last;index'." ) elif clines is not None: - data_len = len(row_body_cells) if "data" in clines else 0 + data_len = len(row_body_cells) if "data" in clines and d["body"] else 0 d["clines"] = defaultdict(list) visible_row_indexes: list[int] = [ From ade24eb5299de2a950b2d674018095c0441698e7 Mon Sep 17 00:00:00 2001 From: Tom Aarsen Date: Thu, 2 Jun 2022 20:39:33 +0000 Subject: [PATCH 2/5] BUG: Tackle GH47203; Add simple test case with empty DataFrames I followed https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#test-structure which seemed to prefer simple functions as tests, rather than embedding them in classes. I can easily change this up if requested. --- pandas/tests/io/formats/test_to_latex.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pandas/tests/io/formats/test_to_latex.py b/pandas/tests/io/formats/test_to_latex.py index f8015851c9a83..b122293a3fdd0 100644 --- a/pandas/tests/io/formats/test_to_latex.py +++ b/pandas/tests/io/formats/test_to_latex.py @@ -1528,3 +1528,14 @@ def test_future_warning(self): ) with tm.assert_produces_warning(FutureWarning, match=msg): df.to_latex() + + +@pytest.mark.parametrize( + "df", + [ + DataFrame(), + DataFrame(columns=["a", "b", "c"]), + ], +) +def test_empty_clines(df: DataFrame): + df.style.to_latex(clines="all;data") From 23fa70f6e988e7fc00a3e0b85ccaf245c9b8075e Mon Sep 17 00:00:00 2001 From: Tom Aarsen Date: Thu, 2 Jun 2022 20:41:24 +0000 Subject: [PATCH 3/5] BUG: Tackle GH47203; Add whatsnew entry --- doc/source/whatsnew/v1.5.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 61848cb127029..5cfa89a39a78f 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -890,6 +890,7 @@ Styler - Bug when attempting to apply styling functions to an empty DataFrame subset (:issue:`45313`) - Bug in :class:`CSSToExcelConverter` leading to ``TypeError`` when border color provided without border style for ``xlsxwriter`` engine (:issue:`42276`) - Bug in :meth:`Styler.set_sticky` leading to white text on white background in dark mode (:issue:`46984`) +- Bug in :meth:`Styler.to_latex` causing ``UnboundLocalError`` when ``clines="all;data"`` and the ``DataFrame`` has no rows. (:issue:`47203`) Metadata ^^^^^^^^ From 46f69f1fa711415671990ee0ef636e7990a476bc Mon Sep 17 00:00:00 2001 From: Tom Aarsen Date: Fri, 3 Jun 2022 07:09:30 +0000 Subject: [PATCH 4/5] Moved tests to `io/formats/style/test_to_latex.py` from `io/formats/test_to_latex.py` --- pandas/tests/io/formats/style/test_to_latex.py | 12 ++++++++++++ pandas/tests/io/formats/test_to_latex.py | 11 ----------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/pandas/tests/io/formats/style/test_to_latex.py b/pandas/tests/io/formats/style/test_to_latex.py index 4f83b3dafcc0b..307fcfc82b17c 100644 --- a/pandas/tests/io/formats/style/test_to_latex.py +++ b/pandas/tests/io/formats/style/test_to_latex.py @@ -1032,3 +1032,15 @@ def test_concat_recursion(): """ ) assert result == expected + + +@pytest.mark.parametrize( + "df", + [ + DataFrame(), + DataFrame(columns=["a", "b", "c"]), + ], +) +def test_empty_clines(df: DataFrame): + # GH 47203 + df.style.to_latex(clines="all;data") diff --git a/pandas/tests/io/formats/test_to_latex.py b/pandas/tests/io/formats/test_to_latex.py index b122293a3fdd0..f8015851c9a83 100644 --- a/pandas/tests/io/formats/test_to_latex.py +++ b/pandas/tests/io/formats/test_to_latex.py @@ -1528,14 +1528,3 @@ def test_future_warning(self): ) with tm.assert_produces_warning(FutureWarning, match=msg): df.to_latex() - - -@pytest.mark.parametrize( - "df", - [ - DataFrame(), - DataFrame(columns=["a", "b", "c"]), - ], -) -def test_empty_clines(df: DataFrame): - df.style.to_latex(clines="all;data") From 124bf0ae5416b75402d2f58516eb07ec12764d6e Mon Sep 17 00:00:00 2001 From: Tom Aarsen Date: Sat, 4 Jun 2022 08:18:42 +0000 Subject: [PATCH 5/5] BUG: GH47203; Add expected return to test cases And parametrize over clines as well. 'skip-last;data' was also broken, after all. --- .../tests/io/formats/style/test_to_latex.py | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/pandas/tests/io/formats/style/test_to_latex.py b/pandas/tests/io/formats/style/test_to_latex.py index 307fcfc82b17c..b295c955a8967 100644 --- a/pandas/tests/io/formats/style/test_to_latex.py +++ b/pandas/tests/io/formats/style/test_to_latex.py @@ -1035,12 +1035,33 @@ def test_concat_recursion(): @pytest.mark.parametrize( - "df", + "df, expected", [ - DataFrame(), - DataFrame(columns=["a", "b", "c"]), + ( + DataFrame(), + dedent( + """\ + \\begin{tabular}{l} + \\end{tabular} + """ + ), + ), + ( + DataFrame(columns=["a", "b", "c"]), + dedent( + """\ + \\begin{tabular}{llll} + & a & b & c \\\\ + \\end{tabular} + """ + ), + ), ], ) -def test_empty_clines(df: DataFrame): +@pytest.mark.parametrize( + "clines", [None, "all;data", "all;index", "skip-last;data", "skip-last;index"] +) +def test_empty_clines(df: DataFrame, expected: str, clines: str): # GH 47203 - df.style.to_latex(clines="all;data") + result = df.style.to_latex(clines=clines) + assert result == expected