Skip to content

Commit 4b12ae3

Browse files
authored
BUG: Styler hidden elements sparse none (#43470)
1 parent 0e92697 commit 4b12ae3

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

doc/source/whatsnew/v1.4.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +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`)
443+
- Bug when combining non-sparse rendering and :meth:`.Styler.hide_columns` or :meth:`.Styler.hide_index` (:issue:`43464`)
444444

445445
Other
446446
^^^^^

pandas/io/formats/style_render.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,7 @@ def _translate_header(
375375
"th",
376376
f"{col_heading_class} level{r} col{c}",
377377
value,
378-
_is_visible(c, r, col_lengths)
379-
and c not in self.hidden_columns,
378+
_is_visible(c, r, col_lengths),
380379
attributes=(
381380
f'colspan="{col_lengths.get((r, c), 0)}"'
382381
if col_lengths.get((r, c), 0) > 1
@@ -534,7 +533,7 @@ def _translate_body(
534533
"th",
535534
f"{row_heading_class} level{c} row{r}",
536535
value,
537-
(_is_visible(r, c, idx_lengths) and not self.hide_index_[c]),
536+
_is_visible(r, c, idx_lengths) and not self.hide_index_[c],
538537
attributes=(
539538
f'rowspan="{idx_lengths.get((c, r), 0)}"'
540539
if idx_lengths.get((c, r), 0) > 1
@@ -948,7 +947,8 @@ def _get_level_lengths(
948947
# stop the loop due to display trimming
949948
break
950949
if not sparsify:
951-
lengths[(i, j)] = 1
950+
if j not in hidden_elements:
951+
lengths[(i, j)] = 1
952952
elif (row is not lib.no_default) and (j not in hidden_elements):
953953
last_label = j
954954
lengths[(i, last_label)] = 1

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

+37-5
Original file line numberDiff line numberDiff line change
@@ -1477,12 +1477,44 @@ def test_caption_raises(mi_styler, caption):
14771477
mi_styler.set_caption(caption)
14781478

14791479

1480-
def test_no_sparse_hiding_columns():
1480+
@pytest.mark.parametrize("axis", ["index", "columns"])
1481+
def test_hiding_headers_over_axis_no_sparsify(axis):
14811482
# GH 43464
14821483
midx = MultiIndex.from_product([[1, 2], ["a", "a", "b"]])
1483-
df = DataFrame(9, index=[0], columns=midx)
1484-
styler = df.style.hide_columns((1, "a"))
1484+
df = DataFrame(
1485+
9,
1486+
index=midx if axis == "index" else [0],
1487+
columns=midx if axis == "columns" else [0],
1488+
)
1489+
1490+
styler = getattr(df.style, f"hide_{axis}")((1, "a"))
14851491
ctx = styler._translate(False, False)
14861492

1487-
for ix in [(0, 1), (0, 2), (1, 1), (1, 2)]:
1488-
assert ctx["head"][ix[0]][ix[1]]["is_visible"] is False
1493+
if axis == "columns": # test column headers
1494+
for ix in [(0, 1), (0, 2), (1, 1), (1, 2)]:
1495+
assert ctx["head"][ix[0]][ix[1]]["is_visible"] is False
1496+
if axis == "index": # test row headers
1497+
for ix in [(0, 0), (0, 1), (1, 0), (1, 1)]:
1498+
assert ctx["body"][ix[0]][ix[1]]["is_visible"] is False
1499+
1500+
1501+
def test_get_level_lengths_mi_hidden():
1502+
# GH 43464
1503+
index = MultiIndex.from_arrays([[1, 1, 1, 2, 2, 2], ["a", "a", "b", "a", "a", "b"]])
1504+
expected = {
1505+
(0, 2): 1,
1506+
(0, 3): 1,
1507+
(0, 4): 1,
1508+
(0, 5): 1,
1509+
(1, 2): 1,
1510+
(1, 3): 1,
1511+
(1, 4): 1,
1512+
(1, 5): 1,
1513+
}
1514+
result = _get_level_lengths(
1515+
index,
1516+
sparsify=False,
1517+
max_index=100,
1518+
hidden_elements=[0, 1, 0, 1], # hidden element can repeat if duplicated index
1519+
)
1520+
tm.assert_dict_equal(result, expected)

0 commit comments

Comments
 (0)