Skip to content

BUG: styler index and empty df #43327

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 4 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ Styler
- Bug in :meth:`.Styler.to_html` where the ``Styler`` object was updated if the ``to_html`` method was called with some args (:issue:`43034`)
- Bug in :meth:`.Styler.copy` where ``uuid`` was not previously copied (:issue:`40675`)
- Bug in :meth:`Styler.apply` where functions which returned Series objects were not correctly handled in terms of aligning their index labels (:issue:`13657`, :issue:`42014`)
-
- Bug when rendering an empty DataFrame with a named index (:issue:`43305`).

Other
^^^^^
Expand Down
4 changes: 3 additions & 1 deletion pandas/io/formats/style_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,9 @@ def _translate_header(
for c, name in enumerate(self.data.index.names)
]

if len(self.data.columns) <= max_cols:
if not clabels:
blank_len = 0
elif len(self.data.columns) <= max_cols:
blank_len = len(clabels[0])
else:
blank_len = len(clabels[0]) + 1 # to allow room for `...` trim col
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/io/formats/style/test_style.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import copy
import re
from textwrap import dedent

import numpy as np
import pytest
Expand Down Expand Up @@ -187,6 +188,23 @@ def test_render_trimming_mi():
assert {"attributes": 'colspan="2"'}.items() <= ctx["head"][0][2].items()


def test_render_empty_mi():
# GH 43305
df = DataFrame(index=MultiIndex.from_product([["A"], [0, 1]], names=[None, "one"]))
expected = dedent(
"""\
>
<thead>
<tr>
<th class="index_name level0" >&nbsp;</th>
<th class="index_name level1" >one</th>
</tr>
</thead>
"""
)
assert expected in df.style.to_html()


@pytest.mark.parametrize("comprehensive", [True, False])
@pytest.mark.parametrize("render", [True, False])
@pytest.mark.parametrize("deepcopy", [True, False])
Expand Down