-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: Styler.background_gradient to accept vmin vmax and dtype Int64 #29245
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
ENH: Styler.background_gradient to accept vmin vmax and dtype Int64 #29245
Conversation
Resolve pandas-dev#12145 and pandas-dev#28869 For `vmin` and `vmax` use the same implementation in `Styler.bar` For dtype `Int64` issue, deprecated `.values` and use `.to_numpy` instead Here explicitly assign the dtype to float since we are doing normalize
pandas/io/formats/style.py
Outdated
@@ -958,6 +958,8 @@ def background_gradient( | |||
axis=0, | |||
subset=None, | |||
text_color_threshold=0.408, | |||
vmin=None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you annotate the new parameters?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your review @WillAyd, but I don't quite get it what do you mean by "annotate", can you elaborate a little more? (I have docstring already so I suppose you don't mean docstring right?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got it! you mean the type-hints annotation right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes that is correct
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done! 😄
pandas/io/formats/style.py
Outdated
smin = s.values.min() | ||
smax = s.values.max() | ||
smin = s.min() if vmin is None else vmin | ||
if isinstance(smin, ABCSeries): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this not already covered by the line directly preceding it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here depends on the axis
argument provided to Styler.background_gradient
, s
can be a DataFrame or a Series. if s
is a DataFrame and vmin
not provided, smin
will first become a Series then become the min value of the full data, the code here is consistent with (aka. direct copy from) Styler.bar
method, do we want to change it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
side note: try to use s.values.min()
to get overall minima will produce AttributeError: 'IntegerArray' object has no attribute 'min'
as described in #28869. OTOH, use np.min(s.values)
will also produce NotImplementedError: The 'reduce' method is not supported.
I thought IntegerArray might be a WIP and .min().min()
is the workaround.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move this into a helper function? I think this affects highlight_extrema
as well but seems to tackle slightly differently; should keep unified
squeeze
might also be more appropriate to use here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! didn't notice the highlight_extrema
, they really should be consistent. turned-out a fairly simple approach can be used: np.nanmin(s.to_numpy())
, s can be any shape
rng = smax - smin | ||
# extend lower / upper bounds, compresses color range | ||
norm = colors.Normalize(smin - (rng * low), smax + (rng * high)) | ||
# matplotlib colors.Normalize modifies inplace? | ||
# https://github.com/matplotlib/matplotlib/issues/5427 | ||
rgbas = plt.cm.get_cmap(cmap)(norm(s.values)) | ||
rgbas = plt.cm.get_cmap(cmap)(norm(s.to_numpy(dtype=float))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK with this but can just use default constructor without dtype
argument I think? As it it seems like this is trying to coerce, which I'm not sure why that would be needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unfortunately, .to_numpy()
alone will produce dtype=object
and rejected by matplotlib.
OTOH, it will immediately convert to float anyway since cmap
require normalizing to [0,1], nothing to loose for coercing in the first place.
…_gradient-to-accept-vmin-vmax-and-int64 sync with the latest upstream/master
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm @TomAugspurger
Thanks @immaxchen! |
…andas-dev#29245) * ENH: Styler.background_gradient to accept vmin vmax and dtype Int64 Resolve pandas-dev#12145 and pandas-dev#28869 For `vmin` and `vmax` use the same implementation in `Styler.bar` For dtype `Int64` issue, deprecated `.values` and use `.to_numpy` instead Here explicitly assign the dtype to float since we are doing normalize
…andas-dev#29245) * ENH: Styler.background_gradient to accept vmin vmax and dtype Int64 Resolve pandas-dev#12145 and pandas-dev#28869 For `vmin` and `vmax` use the same implementation in `Styler.bar` For dtype `Int64` issue, deprecated `.values` and use `.to_numpy` instead Here explicitly assign the dtype to float since we are doing normalize
…andas-dev#29245) * ENH: Styler.background_gradient to accept vmin vmax and dtype Int64 Resolve pandas-dev#12145 and pandas-dev#28869 For `vmin` and `vmax` use the same implementation in `Styler.bar` For dtype `Int64` issue, deprecated `.values` and use `.to_numpy` instead Here explicitly assign the dtype to float since we are doing normalize
For
vmin
vmax
use the same implementation asStyler.bar
For dtype
Int64
issue, deprecated.values
and use.to_numpy
insteadHere explicitly assign the dtype to float since we are doing normalize
style.background_gradient
on anInt64
column #28869black pandas
git diff upstream/master -u -- "*.py" | flake8 --diff