@@ -1296,33 +1296,6 @@ def hide_columns(self, subset) -> Styler:
1296
1296
# A collection of "builtin" styles
1297
1297
# -----------------------------------------------------------------------
1298
1298
1299
- @staticmethod
1300
- def _highlight_null (v , null_color : str ) -> str :
1301
- return f"background-color: { null_color } " if pd .isna (v ) else ""
1302
-
1303
- def highlight_null (
1304
- self ,
1305
- null_color : str = "red" ,
1306
- subset : Optional [IndexLabel ] = None ,
1307
- ) -> Styler :
1308
- """
1309
- Shade the background ``null_color`` for missing values.
1310
-
1311
- Parameters
1312
- ----------
1313
- null_color : str, default 'red'
1314
- subset : label or list of labels, default None
1315
- A valid slice for ``data`` to limit the style application to.
1316
-
1317
- .. versionadded:: 1.1.0
1318
-
1319
- Returns
1320
- -------
1321
- self : Styler
1322
- """
1323
- self .applymap (self ._highlight_null , null_color = null_color , subset = subset )
1324
- return self
1325
-
1326
1299
def background_gradient (
1327
1300
self ,
1328
1301
cmap = "PuBu" ,
@@ -1638,6 +1611,34 @@ def bar(
1638
1611
1639
1612
return self
1640
1613
1614
+ def highlight_null (
1615
+ self ,
1616
+ null_color : str = "red" ,
1617
+ subset : Optional [IndexLabel ] = None ,
1618
+ ) -> Styler :
1619
+ """
1620
+ Shade the background ``null_color`` for missing values.
1621
+
1622
+ Parameters
1623
+ ----------
1624
+ null_color : str, default 'red'
1625
+ subset : label or list of labels, default None
1626
+ A valid slice for ``data`` to limit the style application to.
1627
+
1628
+ .. versionadded:: 1.1.0
1629
+
1630
+ Returns
1631
+ -------
1632
+ self : Styler
1633
+ """
1634
+ return self .apply (
1635
+ _Builtins ._highlight_func ,
1636
+ axis = None ,
1637
+ subset = subset ,
1638
+ props = f"background-color: { null_color } ;" ,
1639
+ highlight = "null" ,
1640
+ )
1641
+
1641
1642
def highlight_max (
1642
1643
self , subset = None , color : str = "yellow" , axis : Optional [Axis ] = 0
1643
1644
) -> Styler :
@@ -1658,7 +1659,13 @@ def highlight_max(
1658
1659
-------
1659
1660
self : Styler
1660
1661
"""
1661
- return self ._highlight_handler (subset = subset , color = color , axis = axis , max_ = True )
1662
+ return self .apply (
1663
+ _Builtins ._highlight_func ,
1664
+ axis = axis ,
1665
+ subset = subset ,
1666
+ props = f"background-color: { color } ;" ,
1667
+ highlight = "max" ,
1668
+ )
1662
1669
1663
1670
def highlight_min (
1664
1671
self , subset = None , color : str = "yellow" , axis : Optional [Axis ] = 0
@@ -1680,43 +1687,13 @@ def highlight_min(
1680
1687
-------
1681
1688
self : Styler
1682
1689
"""
1683
- return self ._highlight_handler (
1684
- subset = subset , color = color , axis = axis , max_ = False
1685
- )
1686
-
1687
- def _highlight_handler (
1688
- self ,
1689
- subset = None ,
1690
- color : str = "yellow" ,
1691
- axis : Optional [Axis ] = None ,
1692
- max_ : bool = True ,
1693
- ) -> Styler :
1694
- subset = non_reducing_slice (maybe_numeric_slice (self .data , subset ))
1695
- self .apply (
1696
- self ._highlight_extrema , color = color , axis = axis , subset = subset , max_ = max_
1690
+ return self .apply (
1691
+ _Builtins ._highlight_func ,
1692
+ axis = axis ,
1693
+ subset = subset ,
1694
+ props = f"background-color: { color } ;" ,
1695
+ highlight = "min" ,
1697
1696
)
1698
- return self
1699
-
1700
- @staticmethod
1701
- def _highlight_extrema (
1702
- data : FrameOrSeries , color : str = "yellow" , max_ : bool = True
1703
- ):
1704
- """
1705
- Highlight the min or max in a Series or DataFrame.
1706
- """
1707
- attr = f"background-color: { color } "
1708
-
1709
- if max_ :
1710
- extrema = data == np .nanmax (data .to_numpy ())
1711
- else :
1712
- extrema = data == np .nanmin (data .to_numpy ())
1713
-
1714
- if data .ndim == 1 : # Series from .apply
1715
- return [attr if v else "" for v in extrema ]
1716
- else : # DataFrame from .tee
1717
- return pd .DataFrame (
1718
- np .where (extrema , attr , "" ), index = data .index , columns = data .columns
1719
- )
1720
1697
1721
1698
@classmethod
1722
1699
def from_custom_template (cls , searchpath , name ):
@@ -1820,6 +1797,26 @@ def pipe(self, func: Callable, *args, **kwargs):
1820
1797
return com .pipe (self , func , * args , ** kwargs )
1821
1798
1822
1799
1800
+ class _Builtins :
1801
+ @staticmethod
1802
+ def _highlight_func (
1803
+ data : FrameOrSeries ,
1804
+ props : str = "background-color: yellow;" ,
1805
+ highlight : str = "max" ,
1806
+ ):
1807
+ """
1808
+ Highlight the value in a Series or DataFrame by func with css-properties
1809
+ """
1810
+ if highlight == "max" :
1811
+ return np .where (data == np .nanmax (data .values ), props , "" )
1812
+ elif highlight == "min" :
1813
+ return np .where (data == np .nanmin (data .values ), props , "" )
1814
+ elif highlight == "null" :
1815
+ return np .where (pd .isna (data ).values , props , "" )
1816
+ else :
1817
+ raise ValueError ("'highlight' must one of 'max', 'min', 'null'" )
1818
+
1819
+
1823
1820
class _Tooltips :
1824
1821
"""
1825
1822
An extension to ``Styler`` that allows for and manipulates tooltips on hover
0 commit comments