Skip to content

BUG: Tackle GH47203; Ensure body is non-empty when accessing row_body_cells #47204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 5, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/formats/style_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/io/formats/style/test_to_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")