Skip to content

BUG: one level multiindex styler display #43385

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 2 commits into from
Sep 4, 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ Styler
- 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`).
- Bug when rendering a single level MultiIndex (:issue:`43383`).

Other
^^^^^
Expand Down
4 changes: 2 additions & 2 deletions pandas/io/formats/style_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ def _translate_body(
)

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

body = []
Expand Down Expand Up @@ -896,7 +896,7 @@ def _get_level_lengths(
hidden_elements = []

lengths = {}
if index.nlevels == 1:
if not isinstance(index, MultiIndex):
for i, value in enumerate(levels):
if i not in hidden_elements:
lengths[(0, i)] = 1
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/io/formats/style/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pandas import (
DataFrame,
IndexSlice,
MultiIndex,
NaT,
Timestamp,
option_context,
Expand Down Expand Up @@ -305,3 +306,14 @@ def test_precision_zero(df):
ctx = styler._translate(True, True)
assert ctx["body"][0][2]["display_value"] == "-1"
assert ctx["body"][1][2]["display_value"] == "-1"


def test_1level_multiindex():
# GH 43383
midx = MultiIndex.from_product([[1, 2]], names=[""])
df = DataFrame(-1, index=midx, columns=[0, 1])
ctx = df.style._translate(True, True)
assert ctx["body"][0][0]["display_value"] == 1
assert ctx["body"][0][0]["is_visible"] is True
assert ctx["body"][1][0]["display_value"] == 2
assert ctx["body"][1][0]["is_visible"] is True