Skip to content

ENH: make levels consistent and name responsive #43570

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 1 commit into from
Sep 14, 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
11 changes: 6 additions & 5 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -1814,7 +1814,7 @@ def set_sticky(
self,
axis: Axis = 0,
pixel_size: int | None = None,
levels: list[int] | None = None,
levels: Level | list[Level] | None = None,
) -> Styler:
"""
Add CSS to permanently display the index or column headers in a scrolling frame.
Expand All @@ -1827,7 +1827,7 @@ def set_sticky(
Required to configure the width of index cells or the height of column
header cells when sticking a MultiIndex (or with a named Index).
Defaults to 75 and 25 respectively.
levels : list of int
levels : int, str, list, optional
If ``axis`` is a MultiIndex the specific levels to stick. If ``None`` will
stick all levels.

Expand Down Expand Up @@ -1891,11 +1891,12 @@ def set_sticky(
else:
# handle the MultiIndex case
range_idx = list(range(obj.nlevels))
levels = sorted(levels) if levels else range_idx
levels_: list[int] = refactor_levels(levels, obj) if levels else range_idx
Copy link
Contributor

Choose a reason for hiding this comment

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

wouldn't object to your moving rfactor_levels to indexing (of course could be a different PR too)

Copy link
Contributor

Choose a reason for hiding this comment

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

but later :->

levels_ = sorted(levels_)

if axis == 1:
styles = []
for i, level in enumerate(levels):
for i, level in enumerate(levels_):
styles.append(
{
"selector": f"thead tr:nth-child({level+1}) th",
Expand All @@ -1920,7 +1921,7 @@ def set_sticky(

else:
styles = []
for i, level in enumerate(levels):
for i, level in enumerate(levels_):
props_ = props + (
f"left:{i * pixel_size}px; "
f"min-width:{pixel_size}px; "
Expand Down
8 changes: 5 additions & 3 deletions pandas/tests/io/formats/style/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,13 @@ def test_sticky_mi(styler_mi, index, columns):

@pytest.mark.parametrize("index", [False, True])
@pytest.mark.parametrize("columns", [False, True])
def test_sticky_levels(styler_mi, index, columns):
@pytest.mark.parametrize("levels", [[1], ["one"], "one"])
def test_sticky_levels(styler_mi, index, columns, levels):
styler_mi.index.names, styler_mi.columns.names = ["zero", "one"], ["zero", "one"]
if index:
styler_mi.set_sticky(axis=0, levels=[1])
styler_mi.set_sticky(axis=0, levels=levels)
if columns:
styler_mi.set_sticky(axis=1, levels=[1])
styler_mi.set_sticky(axis=1, levels=levels)

left_css = (
"#T_ {0} {{\n position: sticky;\n background-color: white;\n"
Expand Down