Skip to content

Commit abea8fa

Browse files
authored
BUG: styler.hide_columns fails when no sparsification (#43465)
1 parent 7d27588 commit abea8fa

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ Styler
440440
- 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`)
441441
- Bug when rendering an empty DataFrame with a named index (:issue:`43305`).
442442
- Bug when rendering a single level MultiIndex (:issue:`43383`).
443+
- Bug when combining non-sparse rendering and :meth:`.Styler.hide_columns` (:issue:`43464`)
443444

444445
Other
445446
^^^^^

pandas/io/formats/style_render.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,8 @@ def _translate_header(
351351
"th",
352352
f"{col_heading_class} level{r} col{c}",
353353
value,
354-
_is_visible(c, r, col_lengths),
354+
_is_visible(c, r, col_lengths)
355+
and c not in self.hidden_columns,
355356
attributes=(
356357
f'colspan="{col_lengths.get((r, c), 0)}"'
357358
if col_lengths.get((r, c), 0) > 1

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

+11
Original file line numberDiff line numberDiff line change
@@ -1447,3 +1447,14 @@ def test_caption_raises(mi_styler, caption):
14471447
msg = "`caption` must be either a string or 2-tuple of strings."
14481448
with pytest.raises(ValueError, match=msg):
14491449
mi_styler.set_caption(caption)
1450+
1451+
1452+
def test_no_sparse_hiding_columns():
1453+
# GH 43464
1454+
midx = MultiIndex.from_product([[1, 2], ["a", "a", "b"]])
1455+
df = DataFrame(9, index=[0], columns=midx)
1456+
styler = df.style.hide_columns((1, "a"))
1457+
ctx = styler._translate(False, False)
1458+
1459+
for ix in [(0, 1), (0, 2), (1, 1), (1, 2)]:
1460+
assert ctx["head"][ix[0]][ix[1]]["is_visible"] is False

0 commit comments

Comments
 (0)