28
28
WriteBuffer ,
29
29
)
30
30
from pandas .compat ._optional import import_optional_dependency
31
- from pandas .util ._decorators import doc
31
+ from pandas .util ._decorators import (
32
+ Substitution ,
33
+ doc ,
34
+ )
32
35
from pandas .util ._exceptions import find_stack_level
33
36
34
37
import pandas as pd
@@ -79,6 +82,26 @@ def _mpl(func: Callable):
79
82
raise ImportError (no_mpl_message .format (func .__name__ ))
80
83
81
84
85
+ ####
86
+ # Shared Doc Strings
87
+
88
+ subset = """
89
+ subset : label, array-like, IndexSlice, optional
90
+ A valid 2d input to `DataFrame.loc[<subset>]`, or, in the case of a 1d input
91
+ or single key, to `DataFrame.loc[:, <subset>]` where the columns are
92
+ prioritised, to limit ``data`` to *before* applying the function.
93
+ """
94
+
95
+ props = """
96
+ props : str, default None
97
+ CSS properties to use for highlighting. If ``props`` is given, ``color``
98
+ is not used.
99
+ """
100
+
101
+ #
102
+ ###
103
+
104
+
82
105
class Styler (StylerRenderer ):
83
106
r"""
84
107
Helps style a DataFrame or Series according to the data with HTML and CSS.
@@ -1307,6 +1330,7 @@ def _apply(
1307
1330
self ._update_ctx (result )
1308
1331
return self
1309
1332
1333
+ @Substitution (subset = subset )
1310
1334
def apply (
1311
1335
self ,
1312
1336
func : Callable ,
@@ -1337,10 +1361,7 @@ def apply(
1337
1361
Apply to each column (``axis=0`` or ``'index'``), to each row
1338
1362
(``axis=1`` or ``'columns'``), or to the entire DataFrame at once
1339
1363
with ``axis=None``.
1340
- subset : label, array-like, IndexSlice, optional
1341
- A valid 2d input to `DataFrame.loc[<subset>]`, or, in the case of a 1d input
1342
- or single key, to `DataFrame.loc[:, <subset>]` where the columns are
1343
- prioritised, to limit ``data`` to *before* applying the function.
1364
+ %(subset)s
1344
1365
**kwargs : dict
1345
1366
Pass along to ``func``.
1346
1367
@@ -1550,6 +1571,7 @@ def _applymap(
1550
1571
self ._update_ctx (result )
1551
1572
return self
1552
1573
1574
+ @Substitution (subset = subset )
1553
1575
def applymap (
1554
1576
self , func : Callable , subset : Subset | None = None , ** kwargs
1555
1577
) -> Styler :
@@ -1562,10 +1584,7 @@ def applymap(
1562
1584
----------
1563
1585
func : function
1564
1586
``func`` should take a scalar and return a string.
1565
- subset : label, array-like, IndexSlice, optional
1566
- A valid 2d input to `DataFrame.loc[<subset>]`, or, in the case of a 1d input
1567
- or single key, to `DataFrame.loc[:, <subset>]` where the columns are
1568
- prioritised, to limit ``data`` to *before* applying the function.
1587
+ %(subset)s
1569
1588
**kwargs : dict
1570
1589
Pass along to ``func``.
1571
1590
@@ -1614,6 +1633,7 @@ def applymap(
1614
1633
)
1615
1634
return self
1616
1635
1636
+ @Substitution (subset = subset )
1617
1637
def where (
1618
1638
self ,
1619
1639
cond : Callable ,
@@ -1639,10 +1659,7 @@ def where(
1639
1659
Applied when ``cond`` returns true.
1640
1660
other : str
1641
1661
Applied when ``cond`` returns false.
1642
- subset : label, array-like, IndexSlice, optional
1643
- A valid 2d input to `DataFrame.loc[<subset>]`, or, in the case of a 1d input
1644
- or single key, to `DataFrame.loc[:, <subset>]` where the columns are
1645
- prioritised, to limit ``data`` to *before* applying the function.
1662
+ %(subset)s
1646
1663
**kwargs : dict
1647
1664
Pass along to ``cond``.
1648
1665
@@ -2532,6 +2549,7 @@ def hide(
2532
2549
axis = "{0 or 'index', 1 or 'columns', None}" ,
2533
2550
text_threshold = "" ,
2534
2551
)
2552
+ @Substitution (subset = subset )
2535
2553
def background_gradient (
2536
2554
self ,
2537
2555
cmap = "PuBu" ,
@@ -2567,10 +2585,7 @@ def background_gradient(
2567
2585
Apply to each column (``axis=0`` or ``'index'``), to each row
2568
2586
(``axis=1`` or ``'columns'``), or to the entire DataFrame at once
2569
2587
with ``axis=None``.
2570
- subset : label, array-like, IndexSlice, optional
2571
- A valid 2d input to `DataFrame.loc[<subset>]`, or, in the case of a 1d input
2572
- or single key, to `DataFrame.loc[:, <subset>]` where the columns are
2573
- prioritised, to limit ``data`` to *before* applying the function.
2588
+ %(subset)s
2574
2589
text_color_threshold : float or int
2575
2590
{text_threshold}
2576
2591
Luminance threshold for determining text color in [0, 1]. Facilitates text
@@ -2722,17 +2737,15 @@ def text_gradient(
2722
2737
text_only = True ,
2723
2738
)
2724
2739
2740
+ @Substitution (subset = subset )
2725
2741
def set_properties (self , subset : Subset | None = None , ** kwargs ) -> Styler :
2726
2742
"""
2727
2743
Set defined CSS-properties to each ``<td>`` HTML element within the given
2728
2744
subset.
2729
2745
2730
2746
Parameters
2731
2747
----------
2732
- subset : label, array-like, IndexSlice, optional
2733
- A valid 2d input to `DataFrame.loc[<subset>]`, or, in the case of a 1d input
2734
- or single key, to `DataFrame.loc[:, <subset>]` where the columns are
2735
- prioritised, to limit ``data`` to *before* applying the function.
2748
+ %(subset)s
2736
2749
**kwargs : dict
2737
2750
A dictionary of property, value pairs to be set for each cell.
2738
2751
@@ -2757,6 +2770,7 @@ def set_properties(self, subset: Subset | None = None, **kwargs) -> Styler:
2757
2770
values = "" .join ([f"{ p } : { v } ;" for p , v in kwargs .items ()])
2758
2771
return self .applymap (lambda x : values , subset = subset )
2759
2772
2773
+ @Substitution (subset = subset )
2760
2774
def bar (
2761
2775
self ,
2762
2776
subset : Subset | None = None ,
@@ -2778,10 +2792,7 @@ def bar(
2778
2792
2779
2793
Parameters
2780
2794
----------
2781
- subset : label, array-like, IndexSlice, optional
2782
- A valid 2d input to `DataFrame.loc[<subset>]`, or, in the case of a 1d input
2783
- or single key, to `DataFrame.loc[:, <subset>]` where the columns are
2784
- prioritised, to limit ``data`` to *before* applying the function.
2795
+ %(subset)s
2785
2796
axis : {0 or 'index', 1 or 'columns', None}, default 0
2786
2797
Apply to each column (``axis=0`` or ``'index'``), to each row
2787
2798
(``axis=1`` or ``'columns'``), or to the entire DataFrame at once
@@ -2882,6 +2893,7 @@ def bar(
2882
2893
2883
2894
return self
2884
2895
2896
+ @Substitution (subset = subset , props = props )
2885
2897
def highlight_null (
2886
2898
self ,
2887
2899
null_color : str = "red" ,
@@ -2894,17 +2906,9 @@ def highlight_null(
2894
2906
Parameters
2895
2907
----------
2896
2908
null_color : str, default 'red'
2897
- subset : label, array-like, IndexSlice, optional
2898
- A valid 2d input to `DataFrame.loc[<subset>]`, or, in the case of a 1d input
2899
- or single key, to `DataFrame.loc[:, <subset>]` where the columns are
2900
- prioritised, to limit ``data`` to *before* applying the function.
2901
-
2909
+ %(subset)s
2902
2910
.. versionadded:: 1.1.0
2903
-
2904
- props : str, default None
2905
- CSS properties to use for highlighting. If ``props`` is given, ``color``
2906
- is not used.
2907
-
2911
+ %(props)s
2908
2912
.. versionadded:: 1.3.0
2909
2913
2910
2914
Returns
@@ -2926,6 +2930,7 @@ def f(data: DataFrame, props: str) -> np.ndarray:
2926
2930
props = f"background-color: { null_color } ;"
2927
2931
return self .apply (f , axis = None , subset = subset , props = props )
2928
2932
2933
+ @Substitution (subset = subset , props = props )
2929
2934
def highlight_max (
2930
2935
self ,
2931
2936
subset : Subset | None = None ,
@@ -2938,20 +2943,14 @@ def highlight_max(
2938
2943
2939
2944
Parameters
2940
2945
----------
2941
- subset : label, array-like, IndexSlice, optional
2942
- A valid 2d input to `DataFrame.loc[<subset>]`, or, in the case of a 1d input
2943
- or single key, to `DataFrame.loc[:, <subset>]` where the columns are
2944
- prioritised, to limit ``data`` to *before* applying the function.
2946
+ %(subset)s
2945
2947
color : str, default 'yellow'
2946
2948
Background color to use for highlighting.
2947
2949
axis : {0 or 'index', 1 or 'columns', None}, default 0
2948
2950
Apply to each column (``axis=0`` or ``'index'``), to each row
2949
2951
(``axis=1`` or ``'columns'``), or to the entire DataFrame at once
2950
2952
with ``axis=None``.
2951
- props : str, default None
2952
- CSS properties to use for highlighting. If ``props`` is given, ``color``
2953
- is not used.
2954
-
2953
+ %(props)s
2955
2954
.. versionadded:: 1.3.0
2956
2955
2957
2956
Returns
@@ -2975,6 +2974,7 @@ def highlight_max(
2975
2974
props = props ,
2976
2975
)
2977
2976
2977
+ @Substitution (subset = subset , props = props )
2978
2978
def highlight_min (
2979
2979
self ,
2980
2980
subset : Subset | None = None ,
@@ -2987,20 +2987,14 @@ def highlight_min(
2987
2987
2988
2988
Parameters
2989
2989
----------
2990
- subset : label, array-like, IndexSlice, optional
2991
- A valid 2d input to `DataFrame.loc[<subset>]`, or, in the case of a 1d input
2992
- or single key, to `DataFrame.loc[:, <subset>]` where the columns are
2993
- prioritised, to limit ``data`` to *before* applying the function.
2990
+ %(subset)s
2994
2991
color : str, default 'yellow'
2995
2992
Background color to use for highlighting.
2996
2993
axis : {0 or 'index', 1 or 'columns', None}, default 0
2997
2994
Apply to each column (``axis=0`` or ``'index'``), to each row
2998
2995
(``axis=1`` or ``'columns'``), or to the entire DataFrame at once
2999
2996
with ``axis=None``.
3000
- props : str, default None
3001
- CSS properties to use for highlighting. If ``props`` is given, ``color``
3002
- is not used.
3003
-
2997
+ %(props)s
3004
2998
.. versionadded:: 1.3.0
3005
2999
3006
3000
Returns
@@ -3024,6 +3018,7 @@ def highlight_min(
3024
3018
props = props ,
3025
3019
)
3026
3020
3021
+ @Substitution (subset = subset , props = props )
3027
3022
def highlight_between (
3028
3023
self ,
3029
3024
subset : Subset | None = None ,
@@ -3041,10 +3036,7 @@ def highlight_between(
3041
3036
3042
3037
Parameters
3043
3038
----------
3044
- subset : label, array-like, IndexSlice, optional
3045
- A valid 2d input to `DataFrame.loc[<subset>]`, or, in the case of a 1d input
3046
- or single key, to `DataFrame.loc[:, <subset>]` where the columns are
3047
- prioritised, to limit ``data`` to *before* applying the function.
3039
+ %(subset)s
3048
3040
color : str, default 'yellow'
3049
3041
Background color to use for highlighting.
3050
3042
axis : {0 or 'index', 1 or 'columns', None}, default 0
@@ -3056,10 +3048,7 @@ def highlight_between(
3056
3048
Right bound for defining the range.
3057
3049
inclusive : {'both', 'neither', 'left', 'right'}
3058
3050
Identify whether bounds are closed or open.
3059
- props : str, default None
3060
- CSS properties to use for highlighting. If ``props`` is given, ``color``
3061
- is not used.
3062
-
3051
+ %(props)s
3063
3052
Returns
3064
3053
-------
3065
3054
self : Styler
@@ -3133,6 +3122,7 @@ def highlight_between(
3133
3122
inclusive = inclusive ,
3134
3123
)
3135
3124
3125
+ @Substitution (subset = subset , props = props )
3136
3126
def highlight_quantile (
3137
3127
self ,
3138
3128
subset : Subset | None = None ,
@@ -3151,10 +3141,7 @@ def highlight_quantile(
3151
3141
3152
3142
Parameters
3153
3143
----------
3154
- subset : label, array-like, IndexSlice, optional
3155
- A valid 2d input to `DataFrame.loc[<subset>]`, or, in the case of a 1d input
3156
- or single key, to `DataFrame.loc[:, <subset>]` where the columns are
3157
- prioritised, to limit ``data`` to *before* applying the function.
3144
+ %(subset)s
3158
3145
color : str, default 'yellow'
3159
3146
Background color to use for highlighting
3160
3147
axis : {0 or 'index', 1 or 'columns', None}, default 0
@@ -3169,10 +3156,7 @@ def highlight_quantile(
3169
3156
quantile estimation.
3170
3157
inclusive : {'both', 'neither', 'left', 'right'}
3171
3158
Identify whether quantile bounds are closed or open.
3172
- props : str, default None
3173
- CSS properties to use for highlighting. If ``props`` is given, ``color``
3174
- is not used.
3175
-
3159
+ %(props)s
3176
3160
Returns
3177
3161
-------
3178
3162
self : Styler
0 commit comments