Skip to content

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

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

Closed
dieterwang opened this issue Apr 15, 2020 · 3 comments · Fixed by #39421
Closed
Labels
Bug Needs Tests Unit test(s) needed to prevent regressions Styler conditional formatting using DataFrame.style
Milestone

Comments

@dieterwang
Copy link

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'
@dieterwang dieterwang added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 15, 2020
@jbrockmendel jbrockmendel added Styler conditional formatting using DataFrame.style and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 6, 2020
@attack68
Copy link
Contributor

attack68 commented Sep 1, 2020

This no longer seems to error on master: 1.2.0.dev0+186.g25a895cf0

@jreback
Copy link
Contributor

jreback commented Sep 1, 2020

@attack68 happy to take a test for this

@jreback jreback added the Needs Tests Unit test(s) needed to prevent regressions label Sep 1, 2020
@jreback jreback added this to the Contributions Welcome milestone Sep 1, 2020
@jimprince
Copy link

Also getting this error but when I'm using the background_gradient method of style.

I started off on 'pandas 1.1.14' -> Issue found, log attached
Upgraded to '1.2.0.dev0+1072.g31db14c48' -> issue persists, log attached
Downgraded to 'v1.2.0.dev0-186-gf46ebe895' to try out approximately when @attack68 says this was working. -> Issue persists.

My code is:

import pandas as pd
import numpy as np

def mklbl(prefix, n):
    return ["%s%s" % (prefix, i) for i in range(n)]

miindex = pd.MultiIndex.from_product([mklbl('A', 5),['D', 2, "x", "J"]])
micolumns = pd.Index(['a', 'foo', 'b', 'bar'])
dfmi = pd.DataFrame(np.arange(len(miindex) * len(micolumns))\
                    .reshape((len(miindex), len(micolumns)))\
                    ,index=miindex,\
                    columns=micolumns\
                   ).sort_index()\
                   .sort_index(axis=1)

slicer = pd.IndexSlice[pd.IndexSlice[:, "D"], :]
print(dfmi.loc[slicer])
dfmi.style.background_gradient(cmap="Red", subset=slicer)

I have a jupyterlab docker container pulled from the official repo running on my MBP.

Thanks in advance to everyone!

Pandas_output1.1.4.txt
Pandas_output1.2.0.dev01072.g31db14c48.txt

@jreback jreback modified the milestones: Contributions Welcome, 1.3 Feb 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Tests Unit test(s) needed to prevent regressions Styler conditional formatting using DataFrame.style
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants