Skip to content

Commit 99ec78c

Browse files
authored
DOC: redocument the subset arg in styler for clarity of use (#41566)
1 parent 70d4697 commit 99ec78c

File tree

2 files changed

+73
-33
lines changed

2 files changed

+73
-33
lines changed

pandas/io/formats/style.py

Lines changed: 69 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -614,9 +614,10 @@ def apply(
614614
Apply to each column (``axis=0`` or ``'index'``), to each row
615615
(``axis=1`` or ``'columns'``), or to the entire DataFrame at once
616616
with ``axis=None``.
617-
subset : IndexSlice
618-
A valid indexer to limit ``data`` to *before* applying the
619-
function. Consider using a pandas.IndexSlice.
617+
subset : label, array-like, IndexSlice, optional
618+
A valid 2d input to `DataFrame.loc[<subset>]`, or, in the case of a 1d input
619+
or single key, to `DataFrame.loc[:, <subset>]` where the columns are
620+
prioritised, to limit ``data`` to *before* applying the function.
620621
**kwargs : dict
621622
Pass along to ``func``.
622623
@@ -642,10 +643,20 @@ def apply(
642643
--------
643644
>>> def highlight_max(x, color):
644645
... return np.where(x == np.nanmax(x.to_numpy()), f"color: {color};", None)
645-
>>> df = pd.DataFrame(np.random.randn(5, 2))
646+
>>> df = pd.DataFrame(np.random.randn(5, 2), columns=["A", "B"])
646647
>>> df.style.apply(highlight_max, color='red')
647648
>>> df.style.apply(highlight_max, color='blue', axis=1)
648649
>>> df.style.apply(highlight_max, color='green', axis=None)
650+
651+
Using ``subset`` to restrict application to a single column or multiple columns
652+
653+
>>> df.style.apply(highlight_max, color='red', subset="A")
654+
>>> df.style.apply(highlight_max, color='red', subset=["A", "B"])
655+
656+
Using a 2d input to ``subset`` to select rows in addition to columns
657+
658+
>>> df.style.apply(highlight_max, color='red', subset=([0,1,2], slice(None))
659+
>>> df.style.apply(highlight_max, color='red', subset=(slice(0,5,2), "A")
649660
"""
650661
self._todo.append(
651662
(lambda instance: getattr(instance, "_apply"), (func, axis, subset), kwargs)
@@ -675,9 +686,10 @@ def applymap(
675686
----------
676687
func : function
677688
``func`` should take a scalar and return a scalar.
678-
subset : IndexSlice
679-
A valid indexer to limit ``data`` to *before* applying the
680-
function. Consider using a pandas.IndexSlice.
689+
subset : label, array-like, IndexSlice, optional
690+
A valid 2d input to `DataFrame.loc[<subset>]`, or, in the case of a 1d input
691+
or single key, to `DataFrame.loc[:, <subset>]` where the columns are
692+
prioritised, to limit ``data`` to *before* applying the function.
681693
**kwargs : dict
682694
Pass along to ``func``.
683695
@@ -699,8 +711,18 @@ def applymap(
699711
--------
700712
>>> def color_negative(v, color):
701713
... return f"color: {color};" if v < 0 else None
702-
>>> df = pd.DataFrame(np.random.randn(5, 2))
714+
>>> df = pd.DataFrame(np.random.randn(5, 2), columns=["A", "B"])
703715
>>> df.style.applymap(color_negative, color='red')
716+
717+
Using ``subset`` to restrict application to a single column or multiple columns
718+
719+
>>> df.style.applymap(color_negative, color='red', subset="A")
720+
>>> df.style.applymap(color_negative, color='red', subset=["A", "B"])
721+
722+
Using a 2d input to ``subset`` to select rows in addition to columns
723+
724+
>>> df.style.applymap(color_negative, color='red', subset=([0,1,2], slice(None))
725+
>>> df.style.applymap(color_negative, color='red', subset=(slice(0,5,2), "A")
704726
"""
705727
self._todo.append(
706728
(lambda instance: getattr(instance, "_applymap"), (func, subset), kwargs)
@@ -732,9 +754,10 @@ def where(
732754
Applied when ``cond`` returns true.
733755
other : str
734756
Applied when ``cond`` returns false.
735-
subset : IndexSlice
736-
A valid indexer to limit ``data`` to *before* applying the
737-
function. Consider using a pandas.IndexSlice.
757+
subset : label, array-like, IndexSlice, optional
758+
A valid 2d input to `DataFrame.loc[<subset>]`, or, in the case of a 1d input
759+
or single key, to `DataFrame.loc[:, <subset>]` where the columns are
760+
prioritised, to limit ``data`` to *before* applying the function.
738761
**kwargs : dict
739762
Pass along to ``cond``.
740763
@@ -1072,9 +1095,9 @@ def hide_columns(self, subset: Subset) -> Styler:
10721095
10731096
Parameters
10741097
----------
1075-
subset : IndexSlice
1076-
An argument to ``DataFrame.loc`` that identifies which columns
1077-
are hidden.
1098+
subset : label, array-like, IndexSlice
1099+
A valid 1d input or single key along the appropriate axis within
1100+
`DataFrame.loc[]`, to limit ``data`` to *before* applying the function.
10781101
10791102
Returns
10801103
-------
@@ -1127,8 +1150,10 @@ def background_gradient(
11271150
Apply to each column (``axis=0`` or ``'index'``), to each row
11281151
(``axis=1`` or ``'columns'``), or to the entire DataFrame at once
11291152
with ``axis=None``.
1130-
subset : IndexSlice
1131-
A valid slice for ``data`` to limit the style application to.
1153+
subset : label, array-like, IndexSlice, optional
1154+
A valid 2d input to `DataFrame.loc[<subset>]`, or, in the case of a 1d input
1155+
or single key, to `DataFrame.loc[:, <subset>]` where the columns are
1156+
prioritised, to limit ``data`` to *before* applying the function.
11321157
text_color_threshold : float or int
11331158
Luminance threshold for determining text color in [0, 1]. Facilitates text
11341159
visibility across varying background colors. All text is dark if 0, and
@@ -1251,8 +1276,10 @@ def set_properties(self, subset: Subset | None = None, **kwargs) -> Styler:
12511276
12521277
Parameters
12531278
----------
1254-
subset : IndexSlice
1255-
A valid slice for ``data`` to limit the style application to.
1279+
subset : label, array-like, IndexSlice, optional
1280+
A valid 2d input to `DataFrame.loc[<subset>]`, or, in the case of a 1d input
1281+
or single key, to `DataFrame.loc[:, <subset>]` where the columns are
1282+
prioritised, to limit ``data`` to *before* applying the function.
12561283
**kwargs : dict
12571284
A dictionary of property, value pairs to be set for each cell.
12581285
@@ -1349,8 +1376,10 @@ def bar(
13491376
13501377
Parameters
13511378
----------
1352-
subset : IndexSlice, optional
1353-
A valid slice for `data` to limit the style application to.
1379+
subset : label, array-like, IndexSlice, optional
1380+
A valid 2d input to `DataFrame.loc[<subset>]`, or, in the case of a 1d input
1381+
or single key, to `DataFrame.loc[:, <subset>]` where the columns are
1382+
prioritised, to limit ``data`` to *before* applying the function.
13541383
axis : {0 or 'index', 1 or 'columns', None}, default 0
13551384
Apply to each column (``axis=0`` or ``'index'``), to each row
13561385
(``axis=1`` or ``'columns'``), or to the entire DataFrame at once
@@ -1431,8 +1460,10 @@ def highlight_null(
14311460
Parameters
14321461
----------
14331462
null_color : str, default 'red'
1434-
subset : label or list of labels, default None
1435-
A valid slice for ``data`` to limit the style application to.
1463+
subset : label, array-like, IndexSlice, optional
1464+
A valid 2d input to `DataFrame.loc[<subset>]`, or, in the case of a 1d input
1465+
or single key, to `DataFrame.loc[:, <subset>]` where the columns are
1466+
prioritised, to limit ``data`` to *before* applying the function.
14361467
14371468
.. versionadded:: 1.1.0
14381469
@@ -1477,8 +1508,10 @@ def highlight_max(
14771508
14781509
Parameters
14791510
----------
1480-
subset : IndexSlice, default None
1481-
A valid slice for ``data`` to limit the style application to.
1511+
subset : label, array-like, IndexSlice, optional
1512+
A valid 2d input to `DataFrame.loc[<subset>]`, or, in the case of a 1d input
1513+
or single key, to `DataFrame.loc[:, <subset>]` where the columns are
1514+
prioritised, to limit ``data`` to *before* applying the function.
14821515
color : str, default 'yellow'
14831516
Background color to use for highlighting.
14841517
axis : {0 or 'index', 1 or 'columns', None}, default 0
@@ -1526,8 +1559,10 @@ def highlight_min(
15261559
15271560
Parameters
15281561
----------
1529-
subset : IndexSlice, default None
1530-
A valid slice for ``data`` to limit the style application to.
1562+
subset : label, array-like, IndexSlice, optional
1563+
A valid 2d input to `DataFrame.loc[<subset>]`, or, in the case of a 1d input
1564+
or single key, to `DataFrame.loc[:, <subset>]` where the columns are
1565+
prioritised, to limit ``data`` to *before* applying the function.
15311566
color : str, default 'yellow'
15321567
Background color to use for highlighting.
15331568
axis : {0 or 'index', 1 or 'columns', None}, default 0
@@ -1580,8 +1615,10 @@ def highlight_between(
15801615
15811616
Parameters
15821617
----------
1583-
subset : IndexSlice, default None
1584-
A valid slice for ``data`` to limit the style application to.
1618+
subset : label, array-like, IndexSlice, optional
1619+
A valid 2d input to `DataFrame.loc[<subset>]`, or, in the case of a 1d input
1620+
or single key, to `DataFrame.loc[:, <subset>]` where the columns are
1621+
prioritised, to limit ``data`` to *before* applying the function.
15851622
color : str, default 'yellow'
15861623
Background color to use for highlighting.
15871624
axis : {0 or 'index', 1 or 'columns', None}, default 0
@@ -1688,8 +1725,10 @@ def highlight_quantile(
16881725
16891726
Parameters
16901727
----------
1691-
subset : IndexSlice, default None
1692-
A valid slice for ``data`` to limit the style application to.
1728+
subset : label, array-like, IndexSlice, optional
1729+
A valid 2d input to `DataFrame.loc[<subset>]`, or, in the case of a 1d input
1730+
or single key, to `DataFrame.loc[:, <subset>]` where the columns are
1731+
prioritised, to limit ``data`` to *before* applying the function.
16931732
color : str, default 'yellow'
16941733
Background color to use for highlighting
16951734
axis : {0 or 'index', 1 or 'columns', None}, default 0

pandas/io/formats/style_render.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,9 +417,10 @@ def format(
417417
----------
418418
formatter : str, callable, dict or None
419419
Object to define how values are displayed. See notes.
420-
subset : IndexSlice
421-
An argument to ``DataFrame.loc`` that restricts which elements
422-
``formatter`` is applied to.
420+
subset : label, array-like, IndexSlice, optional
421+
A valid 2d input to `DataFrame.loc[<subset>]`, or, in the case of a 1d input
422+
or single key, to `DataFrame.loc[:, <subset>]` where the columns are
423+
prioritised, to limit ``data`` to *before* applying the function.
423424
na_rep : str, optional
424425
Representation for missing values.
425426
If ``na_rep`` is None, no special formatting is applied.

0 commit comments

Comments
 (0)