Skip to content

BUG: df.style.apply(subset=...) argument only works for explicit slices but not ":" #33562

Closed
@dieterwang

Description

@dieterwang

Related to #25858 (if not the same?), reproduced on 0.25.3 and current version 1.0.3.

It seems that the subset argument for pd.DataFrame.style only works for slices with explicit labels, e.g. ['A',2] but not for select-all labels, e.g. [:,2]. See MWE below

import pandas as pd
pd.__version__ # 1.0.3

# Taken from 
# https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html
def color_negative_red(val):
    color = 'red' if val < 0 else 'black'
    return 'color: %s' % color

df = pd.DataFrame([
    [-1, 3, 6],
    [-2,-3, 2]
])
df.index = pd.MultiIndex.from_tuples([('A',1),('A',2)])

# Style works for applymap
df.style.applymap(color_negative_red)

# Works for explicit slice
slicer = pd.IndexSlice[pd.IndexSlice['A',2],:]
df.loc[slicer] # valid .loc slicer
df.style.applymap(color_negative_red, subset=slicer) # works

# Breaks for select-all slice
slicer = pd.IndexSlice[pd.IndexSlice[:,2],:]
df.loc[slicer] # valid .loc slicer
df.style.applymap(color_negative_red, subset=slicer)
# TypeError: unhashable type: 'slice'

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds TestsUnit test(s) needed to prevent regressionsStylerconditional formatting using DataFrame.style

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions