Skip to content

Commit 0c3c56b

Browse files
authored
CLN: Styler simplify existing builtin methods (#39797)
1 parent 152f868 commit 0c3c56b

File tree

1 file changed

+48
-65
lines changed

1 file changed

+48
-65
lines changed

pandas/io/formats/style.py

+48-65
Original file line numberDiff line numberDiff line change
@@ -1306,33 +1306,6 @@ def hide_columns(self, subset) -> Styler:
13061306
# A collection of "builtin" styles
13071307
# -----------------------------------------------------------------------
13081308

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-
13361309
def background_gradient(
13371310
self,
13381311
cmap="PuBu",
@@ -1648,8 +1621,39 @@ def bar(
16481621

16491622
return self
16501623

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+
16511652
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,
16531657
) -> Styler:
16541658
"""
16551659
Highlight the maximum by shading the background.
@@ -1668,10 +1672,19 @@ def highlight_max(
16681672
-------
16691673
self : Styler
16701674
"""
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+
)
16721682

16731683
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,
16751688
) -> Styler:
16761689
"""
16771690
Highlight the minimum by shading the background.
@@ -1690,43 +1703,13 @@ def highlight_min(
16901703
-------
16911704
self : Styler
16921705
"""
1693-
return self._highlight_handler(
1694-
subset=subset, color=color, axis=axis, max_=False
1695-
)
16961706

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, "")
17091709

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+
)
17301713

17311714
@classmethod
17321715
def from_custom_template(cls, searchpath, name):

0 commit comments

Comments
 (0)