Skip to content

BUG: Styler hidden elements sparse none #43470

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 3 commits into from
Sep 9, 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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +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`)
- Bug when combining non-sparse rendering and :meth:`.Styler.hide_columns` or :meth:`.Styler.hide_index` (:issue:`43464`)

Other
^^^^^
Expand Down
8 changes: 4 additions & 4 deletions pandas/io/formats/style_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,7 @@ def _translate_header(
"th",
f"{col_heading_class} level{r} col{c}",
value,
_is_visible(c, r, col_lengths)
and c not in self.hidden_columns,
_is_visible(c, r, col_lengths),
attributes=(
f'colspan="{col_lengths.get((r, c), 0)}"'
if col_lengths.get((r, c), 0) > 1
Expand Down Expand Up @@ -534,7 +533,7 @@ def _translate_body(
"th",
f"{row_heading_class} level{c} row{r}",
value,
(_is_visible(r, c, idx_lengths) and not self.hide_index_[c]),
_is_visible(r, c, idx_lengths) and not self.hide_index_[c],
attributes=(
f'rowspan="{idx_lengths.get((c, r), 0)}"'
if idx_lengths.get((c, r), 0) > 1
Expand Down Expand Up @@ -948,7 +947,8 @@ def _get_level_lengths(
# stop the loop due to display trimming
break
if not sparsify:
lengths[(i, j)] = 1
if j not in hidden_elements:
lengths[(i, j)] = 1
elif (row is not lib.no_default) and (j not in hidden_elements):
last_label = j
lengths[(i, last_label)] = 1
Expand Down
42 changes: 37 additions & 5 deletions pandas/tests/io/formats/style/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -1477,12 +1477,44 @@ def test_caption_raises(mi_styler, caption):
mi_styler.set_caption(caption)


def test_no_sparse_hiding_columns():
@pytest.mark.parametrize("axis", ["index", "columns"])
def test_hiding_headers_over_axis_no_sparsify(axis):
# 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"))
df = DataFrame(
9,
index=midx if axis == "index" else [0],
columns=midx if axis == "columns" else [0],
)

styler = getattr(df.style, f"hide_{axis}")((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
if axis == "columns": # test column headers
for ix in [(0, 1), (0, 2), (1, 1), (1, 2)]:
assert ctx["head"][ix[0]][ix[1]]["is_visible"] is False
if axis == "index": # test row headers
for ix in [(0, 0), (0, 1), (1, 0), (1, 1)]:
assert ctx["body"][ix[0]][ix[1]]["is_visible"] is False


def test_get_level_lengths_mi_hidden():
# GH 43464
index = MultiIndex.from_arrays([[1, 1, 1, 2, 2, 2], ["a", "a", "b", "a", "a", "b"]])
expected = {
(0, 2): 1,
(0, 3): 1,
(0, 4): 1,
(0, 5): 1,
(1, 2): 1,
(1, 3): 1,
(1, 4): 1,
(1, 5): 1,
}
result = _get_level_lengths(
index,
sparsify=False,
max_index=100,
hidden_elements=[0, 1, 0, 1], # hidden element can repeat if duplicated index
)
tm.assert_dict_equal(result, expected)