@@ -1306,33 +1306,6 @@ def hide_columns(self, subset) -> Styler:
1306
1306
# A collection of "builtin" styles
1307
1307
# -----------------------------------------------------------------------
1308
1308
1309
- @staticmethod
1310
- def _highlight_null (v , null_color : str ) -> str :
1311
- return f"background-color: { null_color } " if pd .isna (v ) else ""
1312
-
1313
- def highlight_null (
1314
- self ,
1315
- null_color : str = "red" ,
1316
- subset : Optional [IndexLabel ] = None ,
1317
- ) -> Styler :
1318
- """
1319
- Shade the background ``null_color`` for missing values.
1320
-
1321
- Parameters
1322
- ----------
1323
- null_color : str, default 'red'
1324
- subset : label or list of labels, default None
1325
- A valid slice for ``data`` to limit the style application to.
1326
-
1327
- .. versionadded:: 1.1.0
1328
-
1329
- Returns
1330
- -------
1331
- self : Styler
1332
- """
1333
- self .applymap (self ._highlight_null , null_color = null_color , subset = subset )
1334
- return self
1335
-
1336
1309
def background_gradient (
1337
1310
self ,
1338
1311
cmap = "PuBu" ,
@@ -1648,8 +1621,39 @@ def bar(
1648
1621
1649
1622
return self
1650
1623
1624
+ def highlight_null (
1625
+ self ,
1626
+ null_color : str = "red" ,
1627
+ subset : Optional [IndexLabel ] = None ,
1628
+ ) -> Styler :
1629
+ """
1630
+ Shade the background ``null_color`` for missing values.
1631
+
1632
+ Parameters
1633
+ ----------
1634
+ null_color : str, default 'red'
1635
+ subset : label or list of labels, default None
1636
+ A valid slice for ``data`` to limit the style application to.
1637
+
1638
+ .. versionadded:: 1.1.0
1639
+
1640
+ Returns
1641
+ -------
1642
+ self : Styler
1643
+ """
1644
+
1645
+ def f (data : DataFrame , props : str ) -> np .ndarray :
1646
+ return np .where (pd .isna (data ).values , props , "" )
1647
+
1648
+ return self .apply (
1649
+ f , axis = None , subset = subset , props = f"background-color: { null_color } ;"
1650
+ )
1651
+
1651
1652
def highlight_max (
1652
- self , subset = None , color : str = "yellow" , axis : Optional [Axis ] = 0
1653
+ self ,
1654
+ subset : Optional [IndexLabel ] = None ,
1655
+ color : str = "yellow" ,
1656
+ axis : Optional [Axis ] = 0 ,
1653
1657
) -> Styler :
1654
1658
"""
1655
1659
Highlight the maximum by shading the background.
@@ -1668,10 +1672,19 @@ def highlight_max(
1668
1672
-------
1669
1673
self : Styler
1670
1674
"""
1671
- return self ._highlight_handler (subset = subset , color = color , axis = axis , max_ = True )
1675
+
1676
+ def f (data : FrameOrSeries , props : str ) -> np .ndarray :
1677
+ return np .where (data == np .nanmax (data .values ), props , "" )
1678
+
1679
+ return self .apply (
1680
+ f , axis = axis , subset = subset , props = f"background-color: { color } ;"
1681
+ )
1672
1682
1673
1683
def highlight_min (
1674
- self , subset = None , color : str = "yellow" , axis : Optional [Axis ] = 0
1684
+ self ,
1685
+ subset : Optional [IndexLabel ] = None ,
1686
+ color : str = "yellow" ,
1687
+ axis : Optional [Axis ] = 0 ,
1675
1688
) -> Styler :
1676
1689
"""
1677
1690
Highlight the minimum by shading the background.
@@ -1690,43 +1703,13 @@ def highlight_min(
1690
1703
-------
1691
1704
self : Styler
1692
1705
"""
1693
- return self ._highlight_handler (
1694
- subset = subset , color = color , axis = axis , max_ = False
1695
- )
1696
1706
1697
- def _highlight_handler (
1698
- self ,
1699
- subset = None ,
1700
- color : str = "yellow" ,
1701
- axis : Optional [Axis ] = None ,
1702
- max_ : bool = True ,
1703
- ) -> Styler :
1704
- subset = non_reducing_slice (maybe_numeric_slice (self .data , subset ))
1705
- self .apply (
1706
- self ._highlight_extrema , color = color , axis = axis , subset = subset , max_ = max_
1707
- )
1708
- return self
1707
+ def f (data : FrameOrSeries , props : str ) -> np .ndarray :
1708
+ return np .where (data == np .nanmin (data .values ), props , "" )
1709
1709
1710
- @staticmethod
1711
- def _highlight_extrema (
1712
- data : FrameOrSeries , color : str = "yellow" , max_ : bool = True
1713
- ):
1714
- """
1715
- Highlight the min or max in a Series or DataFrame.
1716
- """
1717
- attr = f"background-color: { color } "
1718
-
1719
- if max_ :
1720
- extrema = data == np .nanmax (data .to_numpy ())
1721
- else :
1722
- extrema = data == np .nanmin (data .to_numpy ())
1723
-
1724
- if data .ndim == 1 : # Series from .apply
1725
- return [attr if v else "" for v in extrema ]
1726
- else : # DataFrame from .tee
1727
- return pd .DataFrame (
1728
- np .where (extrema , attr , "" ), index = data .index , columns = data .columns
1729
- )
1710
+ return self .apply (
1711
+ f , axis = axis , subset = subset , props = f"background-color: { color } ;"
1712
+ )
1730
1713
1731
1714
@classmethod
1732
1715
def from_custom_template (cls , searchpath , name ):
0 commit comments