Skip to content

BUG: styler.hide_columns fails when no sparsification #43465

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
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 @@ -440,6 +440,7 @@ Styler
- 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`).
- Bug when combining non-sparse rendering and :meth:`.Styler.hide_columns` (:issue:`43464`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this present in older versions?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nvm i see you did in the OP


Other
^^^^^
Expand Down
3 changes: 2 additions & 1 deletion pandas/io/formats/style_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,8 @@ def _translate_header(
"th",
f"{col_heading_class} level{r} col{c}",
value,
_is_visible(c, r, col_lengths),
_is_visible(c, r, col_lengths)
and c not in self.hidden_columns,
attributes=(
f'colspan="{col_lengths.get((r, c), 0)}"'
if col_lengths.get((r, c), 0) > 1
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/io/formats/style/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -1447,3 +1447,14 @@ def test_caption_raises(mi_styler, caption):
msg = "`caption` must be either a string or 2-tuple of strings."
with pytest.raises(ValueError, match=msg):
mi_styler.set_caption(caption)


def test_no_sparse_hiding_columns():
# GH 43464
midx = MultiIndex.from_product([[1, 2], ["a", "a", "b"]])
df = DataFrame(9, index=[0], columns=midx)
styler = df.style.hide_columns((1, "a"))
ctx = styler._translate(False, False)

for ix in [(0, 1), (0, 2), (1, 1), (1, 2)]:
assert ctx["head"][ix[0]][ix[1]]["is_visible"] is False