From fdeda70dda7aab935aeb33f99a1262550cb01bb5 Mon Sep 17 00:00:00 2001 From: attack68 <24256554+attack68@users.noreply.github.com> Date: Sat, 24 Jul 2021 01:25:52 +0200 Subject: [PATCH] Backport PR #42622: BUG: `Styler.set_sticky` not handling display of `index names`. 1/2 --- doc/source/whatsnew/v1.3.1.rst | 1 + pandas/io/formats/style.py | 54 ++++++++++++++++++---- pandas/tests/io/formats/style/test_html.py | 28 +++++++++-- 3 files changed, 70 insertions(+), 13 deletions(-) diff --git a/doc/source/whatsnew/v1.3.1.rst b/doc/source/whatsnew/v1.3.1.rst index 33f286fccccef..70bfc349607da 100644 --- a/doc/source/whatsnew/v1.3.1.rst +++ b/doc/source/whatsnew/v1.3.1.rst @@ -37,6 +37,7 @@ Bug fixes ~~~~~~~~~ - Fixed bug in :meth:`DataFrame.transpose` dropping values when the DataFrame had an Extension Array dtype and a duplicate index (:issue:`42380`) - Fixed bug in :meth:`DataFrame.to_xml` raising ``KeyError`` when called with ``index=False`` and an offset index (:issue:`42458`) +- Fixed bug in :meth:`.Styler.set_sticky` not handling index names correctly for single index columns case (:issue:`42537`) - Fixed bug in :meth:`DataFrame.copy` failing to consolidate blocks in the result (:issue:`42579`) .. --------------------------------------------------------------------------- diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 12e6c746fd93c..660ca9c58a5b0 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -1452,7 +1452,8 @@ def set_sticky( Whether to make the index or column headers sticky. pixel_size : int, optional Required to configure the width of index cells or the height of column - header cells when sticking a MultiIndex. Defaults to 75 and 25 respectively. + header cells when sticking a MultiIndex (or with a named Index). + Defaults to 75 and 25 respectively. levels : list of int If ``axis`` is a MultiIndex the specific levels to stick. If ``None`` will stick all levels. @@ -1460,6 +1461,16 @@ def set_sticky( Returns ------- self : Styler + + Notes + ----- + This method uses the CSS 'position: sticky;' property to display. It is + designed to work with visible axes, therefore both: + + - `styler.set_sticky(axis="index").hide_index()` + - `styler.set_sticky(axis="columns").hide_columns()` + + may produce strange behaviour due to CSS controls with missing elements. """ if axis in [0, "index"]: axis, obj, tag, pos = 0, self.data.index, "tbody", "left" @@ -1471,15 +1482,42 @@ def set_sticky( raise ValueError("`axis` must be one of {0, 1, 'index', 'columns'}") if not isinstance(obj, pd.MultiIndex): - return self.set_table_styles( - [ + # handling MultiIndexes requires different CSS + props = "position:sticky; background-color:white;" + + if axis == 1: + # stick the first