@@ -91,24 +91,24 @@ def _mpl(func: Callable) -> Generator[tuple[Any, Any], None, None]:
91
91
####
92
92
# Shared Doc Strings
93
93
94
- subset = """subset : label, array-like, IndexSlice, optional
94
+ subset_args = """subset : label, array-like, IndexSlice, optional
95
95
A valid 2d input to `DataFrame.loc[<subset>]`, or, in the case of a 1d input
96
96
or single key, to `DataFrame.loc[:, <subset>]` where the columns are
97
97
prioritised, to limit ``data`` to *before* applying the function."""
98
98
99
- props = """props : str, default None
99
+ properties_args = """props : str, default None
100
100
CSS properties to use for highlighting. If ``props`` is given, ``color``
101
101
is not used."""
102
102
103
- color = """color : str, default '{default}'
103
+ coloring_args = """color : str, default '{default}'
104
104
Background color to use for highlighting."""
105
105
106
- buf = """buf : str, path object, file-like object, optional
106
+ buffering_args = """buf : str, path object, file-like object, optional
107
107
String, path object (implementing ``os.PathLike[str]``), or file-like
108
108
object implementing a string ``write()`` function. If ``None``, the result is
109
109
returned as a string."""
110
110
111
- encoding = """encoding : str, optional
111
+ encoding_args = """encoding : str, optional
112
112
Character encoding setting for file output (and meta tags if available).
113
113
Defaults to ``pandas.options.styler.render.encoding`` value of "utf-8"."""
114
114
@@ -1186,7 +1186,7 @@ def to_html(
1186
1186
) -> str :
1187
1187
...
1188
1188
1189
- @Substitution (buf = buf , encoding = encoding )
1189
+ @Substitution (buf = buffering_args , encoding = encoding_args )
1190
1190
def to_html (
1191
1191
self ,
1192
1192
buf : FilePath | WriteBuffer [str ] | None = None ,
@@ -1345,7 +1345,7 @@ def to_string(
1345
1345
) -> str :
1346
1346
...
1347
1347
1348
- @Substitution (buf = buf , encoding = encoding )
1348
+ @Substitution (buf = buffering_args , encoding = encoding_args )
1349
1349
def to_string (
1350
1350
self ,
1351
1351
buf : FilePath | WriteBuffer [str ] | None = None ,
@@ -1690,7 +1690,7 @@ def _apply(
1690
1690
self ._update_ctx (result )
1691
1691
return self
1692
1692
1693
- @Substitution (subset = subset )
1693
+ @Substitution (subset = subset_args )
1694
1694
def apply (
1695
1695
self ,
1696
1696
func : Callable ,
@@ -1929,7 +1929,7 @@ def _applymap(
1929
1929
self ._update_ctx (result )
1930
1930
return self
1931
1931
1932
- @Substitution (subset = subset )
1932
+ @Substitution (subset = subset_args )
1933
1933
def applymap (
1934
1934
self , func : Callable , subset : Subset | None = None , ** kwargs
1935
1935
) -> Styler :
@@ -2685,7 +2685,7 @@ def _get_numeric_subset_default(self):
2685
2685
visibility across varying background colors. All text is dark if 0, and\n
2686
2686
light if 1, defaults to 0.408.""" ,
2687
2687
)
2688
- @Substitution (subset = subset )
2688
+ @Substitution (subset = subset_args )
2689
2689
def background_gradient (
2690
2690
self ,
2691
2691
cmap : str | Colormap = "PuBu" ,
@@ -2868,7 +2868,7 @@ def text_gradient(
2868
2868
text_only = True ,
2869
2869
)
2870
2870
2871
- @Substitution (subset = subset )
2871
+ @Substitution (subset = subset_args )
2872
2872
def set_properties (self , subset : Subset | None = None , ** kwargs ) -> Styler :
2873
2873
"""
2874
2874
Set defined CSS-properties to each ``<td>`` HTML element for the given subset.
@@ -2900,7 +2900,7 @@ def set_properties(self, subset: Subset | None = None, **kwargs) -> Styler:
2900
2900
values = "" .join ([f"{ p } : { v } ;" for p , v in kwargs .items ()])
2901
2901
return self .applymap (lambda x : values , subset = subset )
2902
2902
2903
- @Substitution (subset = subset )
2903
+ @Substitution (subset = subset_args )
2904
2904
def bar ( # pylint: disable=disallowed-name
2905
2905
self ,
2906
2906
subset : Subset | None = None ,
@@ -3023,7 +3023,11 @@ def bar( # pylint: disable=disallowed-name
3023
3023
3024
3024
return self
3025
3025
3026
- @Substitution (subset = subset , props = props , color = color .format (default = "red" ))
3026
+ @Substitution (
3027
+ subset = subset_args ,
3028
+ props = properties_args ,
3029
+ color = coloring_args .format (default = "red" ),
3030
+ )
3027
3031
def highlight_null (
3028
3032
self ,
3029
3033
color : str = "red" ,
@@ -3066,7 +3070,11 @@ def f(data: DataFrame, props: str) -> np.ndarray:
3066
3070
props = f"background-color: { color } ;"
3067
3071
return self .apply (f , axis = None , subset = subset , props = props )
3068
3072
3069
- @Substitution (subset = subset , color = color .format (default = "yellow" ), props = props )
3073
+ @Substitution (
3074
+ subset = subset_args ,
3075
+ color = coloring_args .format (default = "yellow" ),
3076
+ props = properties_args ,
3077
+ )
3070
3078
def highlight_max (
3071
3079
self ,
3072
3080
subset : Subset | None = None ,
@@ -3110,7 +3118,11 @@ def highlight_max(
3110
3118
props = props ,
3111
3119
)
3112
3120
3113
- @Substitution (subset = subset , color = color .format (default = "yellow" ), props = props )
3121
+ @Substitution (
3122
+ subset = subset_args ,
3123
+ color = coloring_args .format (default = "yellow" ),
3124
+ props = properties_args ,
3125
+ )
3114
3126
def highlight_min (
3115
3127
self ,
3116
3128
subset : Subset | None = None ,
@@ -3154,7 +3166,11 @@ def highlight_min(
3154
3166
props = props ,
3155
3167
)
3156
3168
3157
- @Substitution (subset = subset , color = color .format (default = "yellow" ), props = props )
3169
+ @Substitution (
3170
+ subset = subset_args ,
3171
+ color = coloring_args .format (default = "yellow" ),
3172
+ props = properties_args ,
3173
+ )
3158
3174
def highlight_between (
3159
3175
self ,
3160
3176
subset : Subset | None = None ,
@@ -3258,7 +3274,11 @@ def highlight_between(
3258
3274
inclusive = inclusive ,
3259
3275
)
3260
3276
3261
- @Substitution (subset = subset , color = color .format (default = "yellow" ), props = props )
3277
+ @Substitution (
3278
+ subset = subset_args ,
3279
+ color = coloring_args .format (default = "yellow" ),
3280
+ props = properties_args ,
3281
+ )
3262
3282
def highlight_quantile (
3263
3283
self ,
3264
3284
subset : Subset | None = None ,
@@ -3597,17 +3617,19 @@ def _background_gradient(
3597
3617
else : # else validate gmap against the underlying data
3598
3618
gmap = _validate_apply_axis_arg (gmap , "gmap" , float , data )
3599
3619
3600
- with _mpl (Styler .background_gradient ) as (plt , mpl ):
3620
+ with _mpl (Styler .background_gradient ) as (_ , _matplotlib ):
3601
3621
smin = np .nanmin (gmap ) if vmin is None else vmin
3602
3622
smax = np .nanmax (gmap ) if vmax is None else vmax
3603
3623
rng = smax - smin
3604
3624
# extend lower / upper bounds, compresses color range
3605
- norm = mpl .colors .Normalize (smin - (rng * low ), smax + (rng * high ))
3625
+ norm = _matplotlib .colors .Normalize (smin - (rng * low ), smax + (rng * high ))
3606
3626
3607
3627
if cmap is None :
3608
- rgbas = mpl .colormaps [mpl .rcParams ["image.cmap" ]](norm (gmap ))
3628
+ rgbas = _matplotlib .colormaps [_matplotlib .rcParams ["image.cmap" ]](
3629
+ norm (gmap )
3630
+ )
3609
3631
else :
3610
- rgbas = mpl .colormaps .get_cmap (cmap )(norm (gmap ))
3632
+ rgbas = _matplotlib .colormaps .get_cmap (cmap )(norm (gmap ))
3611
3633
3612
3634
def relative_luminance (rgba ) -> float :
3613
3635
"""
@@ -3636,10 +3658,11 @@ def css(rgba, text_only) -> str:
3636
3658
dark = relative_luminance (rgba ) < text_color_threshold
3637
3659
text_color = "#f1f1f1" if dark else "#000000"
3638
3660
return (
3639
- f"background-color: { mpl .colors .rgb2hex (rgba )} ;color: { text_color } ;"
3661
+ f"background-color: { _matplotlib .colors .rgb2hex (rgba )} ;"
3662
+ + f"color: { text_color } ;"
3640
3663
)
3641
3664
else :
3642
- return f"color: { mpl .colors .rgb2hex (rgba )} ;"
3665
+ return f"color: { _matplotlib .colors .rgb2hex (rgba )} ;"
3643
3666
3644
3667
if data .ndim == 1 :
3645
3668
return [css (rgba , text_only ) for rgba in rgbas ]
@@ -3885,18 +3908,20 @@ def css_calc(x, left: float, right: float, align: str, color: str | list | tuple
3885
3908
rgbas = None
3886
3909
if cmap is not None :
3887
3910
# use the matplotlib colormap input
3888
- with _mpl (Styler .bar ) as (plt , mpl ):
3911
+ with _mpl (Styler .bar ) as (_ , _matplotlib ):
3889
3912
cmap = (
3890
- mpl .colormaps [cmap ]
3913
+ _matplotlib .colormaps [cmap ]
3891
3914
if isinstance (cmap , str )
3892
3915
else cmap # assumed to be a Colormap instance as documented
3893
3916
)
3894
- norm = mpl .colors .Normalize (left , right )
3917
+ norm = _matplotlib .colors .Normalize (left , right )
3895
3918
rgbas = cmap (norm (values ))
3896
3919
if data .ndim == 1 :
3897
- rgbas = [mpl .colors .rgb2hex (rgba ) for rgba in rgbas ]
3920
+ rgbas = [_matplotlib .colors .rgb2hex (rgba ) for rgba in rgbas ]
3898
3921
else :
3899
- rgbas = [[mpl .colors .rgb2hex (rgba ) for rgba in row ] for row in rgbas ]
3922
+ rgbas = [
3923
+ [_matplotlib .colors .rgb2hex (rgba ) for rgba in row ] for row in rgbas
3924
+ ]
3900
3925
3901
3926
assert isinstance (align , str ) # mypy: should now be in [left, right, mid, zero]
3902
3927
if data .ndim == 1 :
0 commit comments