Skip to content

Commit 1b114d7

Browse files
authored
BUG: Tackle GH47203; Ensure body is non-empty when accessing row_body_cells (#47204)
1 parent a54ac87 commit 1b114d7

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

doc/source/whatsnew/v1.5.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,7 @@ Styler
900900
- Bug when attempting to apply styling functions to an empty DataFrame subset (:issue:`45313`)
901901
- Bug in :class:`CSSToExcelConverter` leading to ``TypeError`` when border color provided without border style for ``xlsxwriter`` engine (:issue:`42276`)
902902
- Bug in :meth:`Styler.set_sticky` leading to white text on white background in dark mode (:issue:`46984`)
903+
- Bug in :meth:`Styler.to_latex` causing ``UnboundLocalError`` when ``clines="all;data"`` and the ``DataFrame`` has no rows. (:issue:`47203`)
903904

904905
Metadata
905906
^^^^^^^^

pandas/io/formats/style_render.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,7 @@ def concatenated_visible_rows(obj, n, row_indices):
908908
f"of 'all;data', 'all;index', 'skip-last;data', 'skip-last;index'."
909909
)
910910
elif clines is not None:
911-
data_len = len(row_body_cells) if "data" in clines else 0
911+
data_len = len(row_body_cells) if "data" in clines and d["body"] else 0
912912

913913
d["clines"] = defaultdict(list)
914914
visible_row_indexes: list[int] = [

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

+33
Original file line numberDiff line numberDiff line change
@@ -1032,3 +1032,36 @@ def test_concat_recursion():
10321032
"""
10331033
)
10341034
assert result == expected
1035+
1036+
1037+
@pytest.mark.parametrize(
1038+
"df, expected",
1039+
[
1040+
(
1041+
DataFrame(),
1042+
dedent(
1043+
"""\
1044+
\\begin{tabular}{l}
1045+
\\end{tabular}
1046+
"""
1047+
),
1048+
),
1049+
(
1050+
DataFrame(columns=["a", "b", "c"]),
1051+
dedent(
1052+
"""\
1053+
\\begin{tabular}{llll}
1054+
& a & b & c \\\\
1055+
\\end{tabular}
1056+
"""
1057+
),
1058+
),
1059+
],
1060+
)
1061+
@pytest.mark.parametrize(
1062+
"clines", [None, "all;data", "all;index", "skip-last;data", "skip-last;index"]
1063+
)
1064+
def test_empty_clines(df: DataFrame, expected: str, clines: str):
1065+
# GH 47203
1066+
result = df.style.to_latex(clines=clines)
1067+
assert result == expected

0 commit comments

Comments
 (0)