Skip to content

Commit 2dcaadd

Browse files
authored
BUG: Styler Index when rendering an empty df (#43327)
1 parent ce4d7a3 commit 2dcaadd

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

doc/source/whatsnew/v1.4.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ Styler
381381
- Bug in :meth:`.Styler.to_html` where the ``Styler`` object was updated if the ``to_html`` method was called with some args (:issue:`43034`)
382382
- Bug in :meth:`.Styler.copy` where ``uuid`` was not previously copied (:issue:`40675`)
383383
- 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`)
384-
-
384+
- Bug when rendering an empty DataFrame with a named index (:issue:`43305`).
385385

386386
Other
387387
^^^^^

pandas/io/formats/style_render.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,9 @@ def _translate_header(
393393
for c, name in enumerate(self.data.index.names)
394394
]
395395

396-
if len(self.data.columns) <= max_cols:
396+
if not clabels:
397+
blank_len = 0
398+
elif len(self.data.columns) <= max_cols:
397399
blank_len = len(clabels[0])
398400
else:
399401
blank_len = len(clabels[0]) + 1 # to allow room for `...` trim col

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

+18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import copy
22
import re
3+
from textwrap import dedent
34

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

189190

191+
def test_render_empty_mi():
192+
# GH 43305
193+
df = DataFrame(index=MultiIndex.from_product([["A"], [0, 1]], names=[None, "one"]))
194+
expected = dedent(
195+
"""\
196+
>
197+
<thead>
198+
<tr>
199+
<th class="index_name level0" >&nbsp;</th>
200+
<th class="index_name level1" >one</th>
201+
</tr>
202+
</thead>
203+
"""
204+
)
205+
assert expected in df.style.to_html()
206+
207+
190208
@pytest.mark.parametrize("comprehensive", [True, False])
191209
@pytest.mark.parametrize("render", [True, False])
192210
@pytest.mark.parametrize("deepcopy", [True, False])

0 commit comments

Comments
 (0)