@@ -1374,6 +1374,7 @@ def highlight_null(
1374
1374
Styler.highlight_max: Highlight the maximum with a style.
1375
1375
Styler.highlight_min: Highlight the minimum with a style.
1376
1376
Styler.highlight_between: Highlight a defined range with a style.
1377
+ Styler.highlight_quantile: Highlight values defined by a quantile with a style.
1377
1378
"""
1378
1379
1379
1380
def f (data : DataFrame , props : str ) -> np .ndarray :
@@ -1422,6 +1423,7 @@ def highlight_max(
1422
1423
Styler.highlight_null: Highlight missing values with a style.
1423
1424
Styler.highlight_min: Highlight the minimum with a style.
1424
1425
Styler.highlight_between: Highlight a defined range with a style.
1426
+ Styler.highlight_quantile: Highlight values defined by a quantile with a style.
1425
1427
"""
1426
1428
1427
1429
def f (data : FrameOrSeries , props : str ) -> np .ndarray :
@@ -1470,6 +1472,7 @@ def highlight_min(
1470
1472
Styler.highlight_null: Highlight missing values with a style.
1471
1473
Styler.highlight_max: Highlight the maximum with a style.
1472
1474
Styler.highlight_between: Highlight a defined range with a style.
1475
+ Styler.highlight_quantile: Highlight values defined by a quantile with a style.
1473
1476
"""
1474
1477
1475
1478
def f (data : FrameOrSeries , props : str ) -> np .ndarray :
@@ -1526,6 +1529,7 @@ def highlight_between(
1526
1529
Styler.highlight_null: Highlight missing values with a style.
1527
1530
Styler.highlight_max: Highlight the maximum with a style.
1528
1531
Styler.highlight_min: Highlight the minimum with a style.
1532
+ Styler.highlight_quantile: Highlight values defined by a quantile with a style.
1529
1533
1530
1534
Notes
1531
1535
-----
@@ -1589,6 +1593,110 @@ def highlight_between(
1589
1593
inclusive = inclusive ,
1590
1594
)
1591
1595
1596
+ def highlight_quantile (
1597
+ self ,
1598
+ subset : IndexLabel | None = None ,
1599
+ color : str = "yellow" ,
1600
+ axis : Axis | None = 0 ,
1601
+ q_left : float = 0.0 ,
1602
+ q_right : float = 1.0 ,
1603
+ interpolation : str = "linear" ,
1604
+ inclusive : str = "both" ,
1605
+ props : str | None = None ,
1606
+ ) -> Styler :
1607
+ """
1608
+ Highlight values defined by a quantile with a style.
1609
+
1610
+ .. versionadded:: 1.3.0
1611
+
1612
+ Parameters
1613
+ ----------
1614
+ subset : IndexSlice, default None
1615
+ A valid slice for ``data`` to limit the style application to.
1616
+ color : str, default 'yellow'
1617
+ Background color to use for highlighting
1618
+ axis : {0 or 'index', 1 or 'columns', None}, default 0
1619
+ Axis along which to determine and highlight quantiles. If ``None`` quantiles
1620
+ are measured over the entire DataFrame. See examples.
1621
+ q_left : float, default 0
1622
+ Left bound, in [0, q_right), for the target quantile range.
1623
+ q_right : float, default 1
1624
+ Right bound, in (q_left, 1], for the target quantile range.
1625
+ interpolation : {‘linear’, ‘lower’, ‘higher’, ‘midpoint’, ‘nearest’}
1626
+ Argument passed to ``Series.quantile`` or ``DataFrame.quantile`` for
1627
+ quantile estimation.
1628
+ inclusive : {'both', 'neither', 'left', 'right'}
1629
+ Identify whether quantile bounds are closed or open.
1630
+ props : str, default None
1631
+ CSS properties to use for highlighting. If ``props`` is given, ``color``
1632
+ is not used.
1633
+
1634
+ Returns
1635
+ -------
1636
+ self : Styler
1637
+
1638
+ See Also
1639
+ --------
1640
+ Styler.highlight_null: Highlight missing values with a style.
1641
+ Styler.highlight_max: Highlight the maximum with a style.
1642
+ Styler.highlight_min: Highlight the minimum with a style.
1643
+ Styler.highlight_between: Highlight a defined range with a style.
1644
+
1645
+ Notes
1646
+ -----
1647
+ This function does not work with ``str`` dtypes.
1648
+
1649
+ Examples
1650
+ --------
1651
+ Using ``axis=None`` and apply a quantile to all collective data
1652
+
1653
+ >>> df = pd.DataFrame(np.arange(10).reshape(2,5) + 1)
1654
+ >>> df.style.highlight_quantile(axis=None, q_left=0.8, color="#fffd75")
1655
+
1656
+ .. figure:: ../../_static/style/hq_axNone.png
1657
+
1658
+ Or highlight quantiles row-wise or column-wise, in this case by row-wise
1659
+
1660
+ >>> df.style.highlight_quantile(axis=1, q_left=0.8, color="#fffd75")
1661
+
1662
+ .. figure:: ../../_static/style/hq_ax1.png
1663
+
1664
+ Use ``props`` instead of default background coloring
1665
+
1666
+ >>> df.style.highlight_quantile(axis=None, q_left=0.2, q_right=0.8,
1667
+ ... props='font-weight:bold;color:#e83e8c')
1668
+
1669
+ .. figure:: ../../_static/style/hq_props.png
1670
+ """
1671
+ subset_ = slice (None ) if subset is None else subset
1672
+ subset_ = non_reducing_slice (subset_ )
1673
+ data = self .data .loc [subset_ ]
1674
+
1675
+ # after quantile is found along axis, e.g. along rows,
1676
+ # applying the calculated quantile to alternate axis, e.g. to each column
1677
+ kwargs = {"q" : [q_left , q_right ], "interpolation" : interpolation }
1678
+ if axis in [0 , "index" ]:
1679
+ q = data .quantile (axis = axis , numeric_only = False , ** kwargs )
1680
+ axis_apply : int | None = 1
1681
+ elif axis in [1 , "columns" ]:
1682
+ q = data .quantile (axis = axis , numeric_only = False , ** kwargs )
1683
+ axis_apply = 0
1684
+ else : # axis is None
1685
+ q = Series (data .to_numpy ().ravel ()).quantile (** kwargs )
1686
+ axis_apply = None
1687
+
1688
+ if props is None :
1689
+ props = f"background-color: { color } ;"
1690
+ return self .apply (
1691
+ _highlight_between , # type: ignore[arg-type]
1692
+ axis = axis_apply ,
1693
+ subset = subset ,
1694
+ props = props ,
1695
+ left = q .iloc [0 ],
1696
+ right = q .iloc [1 ],
1697
+ inclusive = inclusive ,
1698
+ )
1699
+
1592
1700
@classmethod
1593
1701
def from_custom_template (cls , searchpath , name ):
1594
1702
"""
0 commit comments