Skip to content

Commit e64784f

Browse files
authored
BUG: one level multiindex styler display (#43385)
1 parent 5e67c78 commit e64784f

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ Styler
425425
- Bug in :meth:`.Styler.copy` where ``uuid`` was not previously copied (:issue:`40675`)
426426
- 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`)
427427
- Bug when rendering an empty DataFrame with a named index (:issue:`43305`).
428+
- Bug when rendering a single level MultiIndex (:issue:`43383`).
428429

429430
Other
430431
^^^^^

pandas/io/formats/style_render.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ def _translate_body(
461461
)
462462

463463
rlabels = self.data.index.tolist()[:max_rows] # slice to allow trimming
464-
if self.data.index.nlevels == 1:
464+
if not isinstance(self.data.index, MultiIndex):
465465
rlabels = [[x] for x in rlabels]
466466

467467
body = []
@@ -896,7 +896,7 @@ def _get_level_lengths(
896896
hidden_elements = []
897897

898898
lengths = {}
899-
if index.nlevels == 1:
899+
if not isinstance(index, MultiIndex):
900900
for i, value in enumerate(levels):
901901
if i not in hidden_elements:
902902
lengths[(0, i)] = 1

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

+12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pandas import (
55
DataFrame,
66
IndexSlice,
7+
MultiIndex,
78
NaT,
89
Timestamp,
910
option_context,
@@ -305,3 +306,14 @@ def test_precision_zero(df):
305306
ctx = styler._translate(True, True)
306307
assert ctx["body"][0][2]["display_value"] == "-1"
307308
assert ctx["body"][1][2]["display_value"] == "-1"
309+
310+
311+
def test_1level_multiindex():
312+
# GH 43383
313+
midx = MultiIndex.from_product([[1, 2]], names=[""])
314+
df = DataFrame(-1, index=midx, columns=[0, 1])
315+
ctx = df.style._translate(True, True)
316+
assert ctx["body"][0][0]["display_value"] == 1
317+
assert ctx["body"][0][0]["is_visible"] is True
318+
assert ctx["body"][1][0]["display_value"] == 2
319+
assert ctx["body"][1][0]["is_visible"] is True

0 commit comments

Comments
 (0)