From e0bfce03f4b7a6e191fab51a58562ebbecd63f21 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Thu, 9 Sep 2021 07:20:07 +0200 Subject: [PATCH 1/2] fix tests --- doc/source/whatsnew/v1.4.0.rst | 2 +- pandas/io/formats/style_render.py | 8 ++++---- pandas/tests/io/formats/style/test_style.py | 20 +++++++++++++++----- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 7107e3eecb2f1..5dff16eef1149 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -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 ^^^^^ diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py index aa91c872f7e97..06e82c0f2c7f8 100644 --- a/pandas/io/formats/style_render.py +++ b/pandas/io/formats/style_render.py @@ -351,8 +351,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 @@ -510,7 +509,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 @@ -908,7 +907,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 diff --git a/pandas/tests/io/formats/style/test_style.py b/pandas/tests/io/formats/style/test_style.py index 1cfcbb5232515..f7bdfbf907099 100644 --- a/pandas/tests/io/formats/style/test_style.py +++ b/pandas/tests/io/formats/style/test_style.py @@ -1449,12 +1449,22 @@ 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 From 7723e5615ddae9e514c39096a731c3448b80b702 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Thu, 9 Sep 2021 07:38:51 +0200 Subject: [PATCH 2/2] add direct test --- pandas/tests/io/formats/style/test_style.py | 22 +++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/pandas/tests/io/formats/style/test_style.py b/pandas/tests/io/formats/style/test_style.py index f7bdfbf907099..4460c527bc5aa 100644 --- a/pandas/tests/io/formats/style/test_style.py +++ b/pandas/tests/io/formats/style/test_style.py @@ -1468,3 +1468,25 @@ def test_hiding_headers_over_axis_no_sparsify(axis): 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)