From 7641c743db6ca6e5e9269fb0a5b576c975980d27 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Fri, 9 Jul 2021 00:52:41 +0200 Subject: [PATCH 01/12] expand hide_index to be multiindex level specific --- pandas/io/formats/style.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index abc0be6419667..c4f58dd7291c1 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -730,7 +730,9 @@ def to_latex( self.data.columns = RangeIndex(stop=len(self.data.columns)) numeric_cols = self.data._get_numeric_data().columns.to_list() self.data.columns = _original_columns - column_format = "" if self.hide_index_ else "l" * self.data.index.nlevels + column_format = "" + for level in range(self.index.nlevels): + column_format += "" if self.hide_index_[level] else "l" for ci, _ in enumerate(self.data.columns): if ci not in self.hidden_columns: column_format += ( @@ -1714,7 +1716,7 @@ def hide_index(self, subset: Subset | None = None) -> Styler: -0.6 1.2 1.8 1.9 0.3 0.3 """ if subset is None: - self.hide_index_ = True + self.hide_index_ = [True for level in range(self.index.nlevels)] else: subset_ = IndexSlice[subset, :] # new var so mypy reads not Optional subset = non_reducing_slice(subset_) From 5561ee599637ce71b29c12da921501fcbb9febd5 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Fri, 9 Jul 2021 00:55:38 +0200 Subject: [PATCH 02/12] expand hide_index to be multiindex level specific --- pandas/io/formats/style_render.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py index e240c04f97ed1..5aac55bfdbeec 100644 --- a/pandas/io/formats/style_render.py +++ b/pandas/io/formats/style_render.py @@ -97,7 +97,7 @@ def __init__( self.cell_ids = cell_ids # add rendering variables - self.hide_index_: bool = False # bools for hiding col/row headers + self.hide_index_: list = [False for level in range(self.index.nlevels)] self.hide_columns_: bool = False self.hidden_rows: Sequence[int] = [] # sequence for specific hidden rows/cols self.hidden_columns: Sequence[int] = [] @@ -306,8 +306,11 @@ def _translate_header( if not self.hide_columns_: for r in range(self.data.columns.nlevels): index_blanks = [ - _element("th", blank_class, blank_value, not self.hide_index_) - ] * (self.data.index.nlevels - 1) + _element( + "th", blank_class, blank_value, not self.hide_index_[level] + ) + for level in range(self.index.nlevels - 1) + ] name = self.data.columns.names[r] column_name = [ @@ -315,7 +318,7 @@ def _translate_header( "th", f"{blank_class if name is None else index_name_class} level{r}", name if name is not None else blank_value, - not self.hide_index_, + not all(self.hide_index_), ) ] @@ -352,14 +355,14 @@ def _translate_header( if ( self.data.index.names and com.any_not_none(*self.data.index.names) - and not self.hide_index_ + and not all(self.hide_index_) ): index_names = [ _element( "th", f"{index_name_class} level{c}", blank_value if name is None else name, - True, + not self.hide_index_[c], ) for c, name in enumerate(self.data.index.names) ] @@ -434,7 +437,7 @@ def _translate_body( "th", f"{row_heading_class} level{c} {trimmed_row_class}", "...", - not self.hide_index_, + not self.hide_index_[c], attributes="", ) for c in range(self.data.index.nlevels) @@ -471,7 +474,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_), + (_is_visible(r, c, idx_lengths) and not self.hide_index_[c]), id=f"level{c}_row{r}", attributes=( f'rowspan="{idx_lengths.get((c, r), 0)}"' @@ -536,7 +539,7 @@ def _translate_latex(self, d: dict) -> None: d["head"] = [[col for col in row if col["is_visible"]] for row in d["head"]] body = [] for r, row in enumerate(d["body"]): - if self.hide_index_: + if all(self.hide_index_): row_body_headers = [] else: row_body_headers = [ From 0d37b82a7cc0f94a992375f54d8c6bc15d179049 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Fri, 9 Jul 2021 10:51:30 +0200 Subject: [PATCH 03/12] code level functionality --- pandas/io/formats/style.py | 33 ++++++++++++++++++--- pandas/io/formats/style_render.py | 11 +++---- pandas/tests/io/formats/style/test_style.py | 29 +++++++++++++++--- 3 files changed, 58 insertions(+), 15 deletions(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index c4f58dd7291c1..8cb4368d023ef 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -1647,14 +1647,18 @@ def set_na_rep(self, na_rep: str) -> StylerRenderer: self.na_rep = na_rep return self.format(na_rep=na_rep, precision=self.precision) - def hide_index(self, subset: Subset | None = None) -> Styler: + def hide_index( + self, + subset: Subset | None = None, + level: int | str | list[int | str] | None = None, + ) -> Styler: """ Hide the entire index, or specific keys in the index from rendering. This method has dual functionality: - - if ``subset`` is ``None`` then the entire index will be hidden whilst - displaying all data-rows. + - if ``subset`` is ``None`` then the entire index, or specified levels, will + be hidden whilst displaying all data-rows. - if a ``subset`` is given then those specific rows will be hidden whilst the index itself remains visible. @@ -1666,6 +1670,9 @@ def hide_index(self, subset: Subset | None = None) -> Styler: A valid 1d input or single key along the index axis within `DataFrame.loc[, :]`, to limit ``data`` to *before* applying the function. + level : int, str, list + The level(s) to hide in a MultiIndex if hiding the entire index. Cannot be + used simultaneously with ``subset``. Returns ------- @@ -1715,8 +1722,26 @@ def hide_index(self, subset: Subset | None = None) -> Styler: 0.7 1.0 1.3 1.5 -0.0 -0.2 -0.6 1.2 1.8 1.9 0.3 0.3 """ + if level is not None and subset is not None: + raise ValueError("`subset` and `level` cannot be passed simultaneously") + if subset is None: - self.hide_index_ = [True for level in range(self.index.nlevels)] + if level is None: + levels: list[int] = list(range(self.index.nlevels)) + elif isinstance(level, int): + levels = [level] + elif isinstance(level, str): + levels = [self.index._get_level_number(level)] + elif isinstance(level, list): + levels = [ + self.index._get_level_number(lev) if isinstance(lev, str) else lev + for lev in level + ] + else: + raise ValueError("`level` must be of type `int`, `str` or list of such") + self.hide_index_ = [ + True if lev in levels else False for lev in range(self.index.nlevels) + ] else: subset_ = IndexSlice[subset, :] # new var so mypy reads not Optional subset = non_reducing_slice(subset_) diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py index 5aac55bfdbeec..b626251eaffae 100644 --- a/pandas/io/formats/style_render.py +++ b/pandas/io/formats/style_render.py @@ -97,7 +97,7 @@ def __init__( self.cell_ids = cell_ids # add rendering variables - self.hide_index_: list = [False for level in range(self.index.nlevels)] + self.hide_index_: list = [False] * self.index.nlevels self.hide_columns_: bool = False self.hidden_rows: Sequence[int] = [] # sequence for specific hidden rows/cols self.hidden_columns: Sequence[int] = [] @@ -305,12 +305,9 @@ def _translate_header( # 1) column headers if not self.hide_columns_: for r in range(self.data.columns.nlevels): - index_blanks = [ - _element( - "th", blank_class, blank_value, not self.hide_index_[level] - ) - for level in range(self.index.nlevels - 1) - ] + index_blanks = [_element("th", blank_class, blank_value, True)] * ( + self.index.nlevels - sum(self.hide_index_) - 1 + ) name = self.data.columns.names[r] column_name = [ diff --git a/pandas/tests/io/formats/style/test_style.py b/pandas/tests/io/formats/style/test_style.py index 480356de2450f..895aaeb794acd 100644 --- a/pandas/tests/io/formats/style/test_style.py +++ b/pandas/tests/io/formats/style/test_style.py @@ -260,6 +260,26 @@ def test_clear(mi_styler_comp): assert all(res) if hasattr(res, "__iter__") else res +def test_hide_raises(mi_styler): + msg = "`subset` and `level` cannot be passed simultaneously" + with pytest.raises(ValueError, match=msg): + mi_styler.hide_index(subset="something", level="something else") + + msg = "`level` must be of type `int`, `str` or list of such" + with pytest.raises(ValueError, match=msg): + mi_styler.hide_index(level=("bad", "type")) + + +@pytest.mark.parametrize("level", [1, "one", [1], ["one"]]) +def test_hide_level(mi_styler, level): + mi_styler.index.names, mi_styler.columns.names = ["zero", "one"], ["zero", "one"] + ctx = mi_styler.hide_index(level=level)._translate(True, True) + assert ctx["body"][0][0]["is_visible"] + assert not ctx["body"][0][1]["is_visible"] + assert ctx["body"][1] == "f" + assert not ctx["body"][1][1]["is_visible"] + + class TestStyler: def setup_method(self, method): np.random.seed(24) @@ -1140,7 +1160,7 @@ def test_hide_single_index(self): def test_hide_multiindex(self): # GH 14194 df = DataFrame( - {"A": [1, 2]}, + {"A": [1, 2], "B": [1, 2]}, index=MultiIndex.from_arrays( [["a", "a"], [0, 1]], names=["idx_level_0", "idx_level_1"] ), @@ -1150,16 +1170,17 @@ def test_hide_multiindex(self): assert ctx1["body"][0][0]["is_visible"] assert ctx1["body"][0][1]["is_visible"] # check for blank header rows - assert ctx1["head"][0][0]["is_visible"] - assert ctx1["head"][0][1]["is_visible"] + print(ctx1["head"][0]) + assert len(ctx1["head"][0]) == 4 # two visible indexes and two data columns ctx2 = df.style.hide_index()._translate(True, True) # tests for 'a' and '0' assert not ctx2["body"][0][0]["is_visible"] assert not ctx2["body"][0][1]["is_visible"] # check for blank header rows + print(ctx2["head"][0]) + assert len(ctx2["head"][0]) == 3 # one hidden (col name) and two data columns assert not ctx2["head"][0][0]["is_visible"] - assert not ctx2["head"][0][1]["is_visible"] def test_hide_columns_single_level(self): # GH 14194 From 31a5217d90a2e1df41e292597f754edceec87bd1 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Tue, 20 Jul 2021 21:37:56 +0200 Subject: [PATCH 04/12] change kw:level to levels, fix tests --- pandas/io/formats/style.py | 32 +++++++++++---------- pandas/io/formats/style_render.py | 1 + pandas/tests/io/formats/style/test_style.py | 23 ++++++++------- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 7480baa729394..063c3960802e6 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -1650,7 +1650,7 @@ def set_na_rep(self, na_rep: str) -> StylerRenderer: def hide_index( self, subset: Subset | None = None, - level: int | str | list[int | str] | None = None, + levels: int | str | list[int | str] | None = None, ) -> Styler: """ Hide the entire index, or specific keys in the index from rendering. @@ -1670,7 +1670,7 @@ def hide_index( A valid 1d input or single key along the index axis within `DataFrame.loc[, :]`, to limit ``data`` to *before* applying the function. - level : int, str, list + levels : int, str, list The level(s) to hide in a MultiIndex if hiding the entire index. Cannot be used simultaneously with ``subset``. @@ -1722,25 +1722,27 @@ def hide_index( 0.7 1.0 1.3 1.5 -0.0 -0.2 -0.6 1.2 1.8 1.9 0.3 0.3 """ - if level is not None and subset is not None: - raise ValueError("`subset` and `level` cannot be passed simultaneously") + if levels is not None and subset is not None: + raise ValueError("`subset` and `levels` cannot be passed simultaneously") if subset is None: - if level is None: - levels: list[int] = list(range(self.index.nlevels)) - elif isinstance(level, int): - levels = [level] - elif isinstance(level, str): - levels = [self.index._get_level_number(level)] - elif isinstance(level, list): - levels = [ + if levels is None: + levels_: list[int] = list(range(self.index.nlevels)) + elif isinstance(levels, int): + levels_ = [levels] + elif isinstance(levels, str): + levels_ = [self.index._get_level_number(levels)] + elif isinstance(levels, list): + levels_ = [ self.index._get_level_number(lev) if isinstance(lev, str) else lev - for lev in level + for lev in levels ] else: - raise ValueError("`level` must be of type `int`, `str` or list of such") + raise ValueError( + "`levels` must be of type `int`, `str` or list of such" + ) self.hide_index_ = [ - True if lev in levels else False for lev in range(self.index.nlevels) + True if lev in levels_ else False for lev in range(self.index.nlevels) ] else: subset_ = IndexSlice[subset, :] # new var so mypy reads not Optional diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py index b626251eaffae..6a16e62020094 100644 --- a/pandas/io/formats/style_render.py +++ b/pandas/io/formats/style_render.py @@ -305,6 +305,7 @@ def _translate_header( # 1) column headers if not self.hide_columns_: for r in range(self.data.columns.nlevels): + # number of index blanks is governed by number of hidden index levels index_blanks = [_element("th", blank_class, blank_value, True)] * ( self.index.nlevels - sum(self.hide_index_) - 1 ) diff --git a/pandas/tests/io/formats/style/test_style.py b/pandas/tests/io/formats/style/test_style.py index 3856aed747d37..5d8c5a9108c6f 100644 --- a/pandas/tests/io/formats/style/test_style.py +++ b/pandas/tests/io/formats/style/test_style.py @@ -261,22 +261,22 @@ def test_clear(mi_styler_comp): def test_hide_raises(mi_styler): - msg = "`subset` and `level` cannot be passed simultaneously" + msg = "`subset` and `levels` cannot be passed simultaneously" with pytest.raises(ValueError, match=msg): - mi_styler.hide_index(subset="something", level="something else") + mi_styler.hide_index(subset="something", levels="something else") - msg = "`level` must be of type `int`, `str` or list of such" + msg = "`levels` must be of type `int`, `str` or list of such" with pytest.raises(ValueError, match=msg): - mi_styler.hide_index(level=("bad", "type")) + mi_styler.hide_index(levels=("bad", "type")) -@pytest.mark.parametrize("level", [1, "one", [1], ["one"]]) -def test_hide_level(mi_styler, level): +@pytest.mark.parametrize("levels", [1, "one", [1], ["one"]]) +def test_hide_level(mi_styler, levels): mi_styler.index.names, mi_styler.columns.names = ["zero", "one"], ["zero", "one"] - ctx = mi_styler.hide_index(level=level)._translate(True, True) + ctx = mi_styler.hide_index(levels=levels)._translate(False, True) assert ctx["body"][0][0]["is_visible"] assert not ctx["body"][0][1]["is_visible"] - assert ctx["body"][1] == "f" + assert ctx["body"][1][0]["is_visible"] assert not ctx["body"][1][1]["is_visible"] @@ -1264,9 +1264,10 @@ def test_hide_columns_index_mult_levels(self): # hide second column and index ctx = df.style.hide_columns([("b", 1)]).hide_index()._translate(True, True) assert not ctx["body"][0][0]["is_visible"] # index - assert ctx["head"][0][2]["is_visible"] # b - assert ctx["head"][1][2]["is_visible"] # 0 - assert not ctx["head"][1][3]["is_visible"] # 1 + assert len(ctx["head"][0]) == 3 + assert ctx["head"][0][1]["is_visible"] # b + assert ctx["head"][1][1]["is_visible"] # 0 + assert not ctx["head"][1][2]["is_visible"] # 1 assert not ctx["body"][1][3]["is_visible"] # 4 assert ctx["body"][1][2]["is_visible"] assert ctx["body"][1][2]["display_value"] == 3 From f0991f094c7a8ae69bb2a138e3b07b2add693b4f Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Tue, 20 Jul 2021 23:59:19 +0200 Subject: [PATCH 05/12] whats new --- doc/source/whatsnew/v1.4.0.rst | 1 + pandas/tests/io/formats/style/test_style.py | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 8d96d49daba4f..f4346a22e10be 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -33,6 +33,7 @@ Other enhancements - :meth:`Series.sample`, :meth:`DataFrame.sample`, and :meth:`.GroupBy.sample` now accept a ``np.random.Generator`` as input to ``random_state``. A generator will be more performant, especially with ``replace=False`` (:issue:`38100`) - Additional options added to :meth:`.Styler.bar` to control alignment and display, with keyword only arguments (:issue:`26070`, :issue:`36419`) - :meth:`Styler.bar` now validates the input argument ``width`` and ``height`` (:issue:`42511`) +- Add keyword ``levels`` to :meth:`.Styler.hide_index` for optionally controlling hidden levels in a MultiIndex (:issue:`25475) - :meth:`Series.ewm`, :meth:`DataFrame.ewm`, now support a ``method`` argument with a ``'table'`` option that performs the windowing operation over an entire :class:`DataFrame`. See :ref:`Window Overview ` for performance and functional benefits (:issue:`42273`) - diff --git a/pandas/tests/io/formats/style/test_style.py b/pandas/tests/io/formats/style/test_style.py index 5d8c5a9108c6f..ac9ffcfc3cb5c 100644 --- a/pandas/tests/io/formats/style/test_style.py +++ b/pandas/tests/io/formats/style/test_style.py @@ -274,6 +274,12 @@ def test_hide_raises(mi_styler): def test_hide_level(mi_styler, levels): mi_styler.index.names, mi_styler.columns.names = ["zero", "one"], ["zero", "one"] ctx = mi_styler.hide_index(levels=levels)._translate(False, True) + assert len(ctx["head"][0]) == 3 + assert len(ctx["head"][1]) == 3 + assert len(ctx["head"][2]) == 4 + assert ctx["head"][2][0]["is_visible"] + assert not ctx["head"][2][1]["is_visible"] + assert ctx["body"][0][0]["is_visible"] assert not ctx["body"][0][1]["is_visible"] assert ctx["body"][1][0]["is_visible"] From d747715eaee4dc082088b5ec8fa02f38b1f3dbcd Mon Sep 17 00:00:00 2001 From: attack68 <24256554+attack68@users.noreply.github.com> Date: Wed, 21 Jul 2021 14:07:34 +0200 Subject: [PATCH 06/12] Update v1.4.0.rst --- doc/source/whatsnew/v1.4.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index f4346a22e10be..c200627ad66f8 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -33,7 +33,7 @@ Other enhancements - :meth:`Series.sample`, :meth:`DataFrame.sample`, and :meth:`.GroupBy.sample` now accept a ``np.random.Generator`` as input to ``random_state``. A generator will be more performant, especially with ``replace=False`` (:issue:`38100`) - Additional options added to :meth:`.Styler.bar` to control alignment and display, with keyword only arguments (:issue:`26070`, :issue:`36419`) - :meth:`Styler.bar` now validates the input argument ``width`` and ``height`` (:issue:`42511`) -- Add keyword ``levels`` to :meth:`.Styler.hide_index` for optionally controlling hidden levels in a MultiIndex (:issue:`25475) +- Add keyword ``levels`` to :meth:`.Styler.hide_index` for optionally controlling hidden levels in a MultiIndex (:issue:`25475`) - :meth:`Series.ewm`, :meth:`DataFrame.ewm`, now support a ``method`` argument with a ``'table'`` option that performs the windowing operation over an entire :class:`DataFrame`. See :ref:`Window Overview ` for performance and functional benefits (:issue:`42273`) - From 2c0363b9a11f1e578fea30993f17ce06b7d5144c Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sun, 1 Aug 2021 09:10:20 +0200 Subject: [PATCH 07/12] typing on Level: jreback request --- pandas/io/formats/style.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index de6cbfffe742e..e2abca6e8b509 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -24,6 +24,7 @@ FilePathOrBuffer, FrameOrSeries, IndexLabel, + Level, Scalar, ) from pandas.compat._optional import import_optional_dependency @@ -1751,7 +1752,7 @@ def set_na_rep(self, na_rep: str) -> StylerRenderer: def hide_index( self, subset: Subset | None = None, - levels: int | str | list[int | str] | None = None, + levels: Level | list[Level] | None = None, ) -> Styler: """ Hide the entire index, or specific keys in the index from rendering. @@ -1829,14 +1830,16 @@ def hide_index( if subset is None: if levels is None: - levels_: list[int] = list(range(self.index.nlevels)) + levels_: list[Level] = list(range(self.index.nlevels)) elif isinstance(levels, int): levels_ = [levels] elif isinstance(levels, str): levels_ = [self.index._get_level_number(levels)] elif isinstance(levels, list): levels_ = [ - self.index._get_level_number(lev) if isinstance(lev, str) else lev + self.index._get_level_number(lev) + if not isinstance(lev, int) + else lev for lev in levels ] else: From 67a27edb1fa342f4c9413f9e9c51f82032dd8389 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sun, 1 Aug 2021 09:12:51 +0200 Subject: [PATCH 08/12] versionadded: jreback request --- pandas/io/formats/style.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index e2abca6e8b509..75f1ede200bb1 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -1776,6 +1776,8 @@ def hide_index( The level(s) to hide in a MultiIndex if hiding the entire index. Cannot be used simultaneously with ``subset``. + .. versionadded:: 1.4.0 + Returns ------- self : Styler From f80eb96d74e1f7dc86914a424ec8d3833a612ecf Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sun, 1 Aug 2021 09:19:38 +0200 Subject: [PATCH 09/12] test clean --- pandas/tests/io/formats/style/test_style.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/io/formats/style/test_style.py b/pandas/tests/io/formats/style/test_style.py index ac9ffcfc3cb5c..75a74a6220514 100644 --- a/pandas/tests/io/formats/style/test_style.py +++ b/pandas/tests/io/formats/style/test_style.py @@ -267,7 +267,7 @@ def test_hide_raises(mi_styler): msg = "`levels` must be of type `int`, `str` or list of such" with pytest.raises(ValueError, match=msg): - mi_styler.hide_index(levels=("bad", "type")) + mi_styler.hide_index(levels={"bad": 1, "type": 2}) @pytest.mark.parametrize("levels", [1, "one", [1], ["one"]]) From 1a61af89806279b4ca2f8fe206961250a59f0ca2 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sun, 1 Aug 2021 20:44:25 +0200 Subject: [PATCH 10/12] add another example --- pandas/io/formats/style.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 75f1ede200bb1..1a1c50767650f 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -1826,6 +1826,18 @@ def hide_index( a b c a b c 0.7 1.0 1.3 1.5 -0.0 -0.2 -0.6 1.2 1.8 1.9 0.3 0.3 + + Hide a specific level: + + >>> df.style.format("{:,.1f").hide_index(levels=1) + x y + a b c a b c + x 0.1 0.0 0.4 1.3 0.6 -1.4 + 0.7 1.0 1.3 1.5 -0.0 -0.2 + 1.4 -0.8 1.6 -0.2 -0.4 -0.3 + y 0.4 1.0 -0.2 -0.8 -1.2 1.1 + -0.6 1.2 1.8 1.9 0.3 0.3 + 0.8 0.5 -0.3 1.2 2.2 -0.8 """ if levels is not None and subset is not None: raise ValueError("`subset` and `levels` cannot be passed simultaneously") From 7faf1d3d62ccd3d0c42ccad42d7b748a6b60ff21 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sun, 1 Aug 2021 20:44:43 +0200 Subject: [PATCH 11/12] add another example --- pandas/io/formats/style.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 1a1c50767650f..829cb6cf3af4e 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -1830,14 +1830,14 @@ def hide_index( Hide a specific level: >>> df.style.format("{:,.1f").hide_index(levels=1) - x y - a b c a b c - x 0.1 0.0 0.4 1.3 0.6 -1.4 - 0.7 1.0 1.3 1.5 -0.0 -0.2 - 1.4 -0.8 1.6 -0.2 -0.4 -0.3 - y 0.4 1.0 -0.2 -0.8 -1.2 1.1 - -0.6 1.2 1.8 1.9 0.3 0.3 - 0.8 0.5 -0.3 1.2 2.2 -0.8 + x y + a b c a b c + x 0.1 0.0 0.4 1.3 0.6 -1.4 + 0.7 1.0 1.3 1.5 -0.0 -0.2 + 1.4 -0.8 1.6 -0.2 -0.4 -0.3 + y 0.4 1.0 -0.2 -0.8 -1.2 1.1 + -0.6 1.2 1.8 1.9 0.3 0.3 + 0.8 0.5 -0.3 1.2 2.2 -0.8 """ if levels is not None and subset is not None: raise ValueError("`subset` and `levels` cannot be passed simultaneously") From c390327f970a566d3f2ad9c914a37c083f1b0e8e Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sun, 1 Aug 2021 20:46:04 +0200 Subject: [PATCH 12/12] doctest skip --- pandas/io/formats/style.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 829cb6cf3af4e..3688073e7ba90 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -1829,7 +1829,7 @@ def hide_index( Hide a specific level: - >>> df.style.format("{:,.1f").hide_index(levels=1) + >>> df.style.format("{:,.1f").hide_index(levels=1) # doctest: +SKIP x y a b c a b c x 0.1 0.0 0.4 1.3 0.6 -1.4