Skip to content

Commit 10d01ca

Browse files
committed
CLN: compact styler builtins after axis=none allows ndarray
1 parent a85b3b3 commit 10d01ca

File tree

1 file changed

+61
-64
lines changed

1 file changed

+61
-64
lines changed

pandas/io/formats/style.py

Lines changed: 61 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,33 +1296,6 @@ def hide_columns(self, subset) -> Styler:
12961296
# A collection of "builtin" styles
12971297
# -----------------------------------------------------------------------
12981298

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-
13261299
def background_gradient(
13271300
self,
13281301
cmap="PuBu",
@@ -1638,6 +1611,34 @@ def bar(
16381611

16391612
return self
16401613

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+
16411642
def highlight_max(
16421643
self, subset=None, color: str = "yellow", axis: Optional[Axis] = 0
16431644
) -> Styler:
@@ -1658,7 +1659,13 @@ def highlight_max(
16581659
-------
16591660
self : Styler
16601661
"""
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+
)
16621669

16631670
def highlight_min(
16641671
self, subset=None, color: str = "yellow", axis: Optional[Axis] = 0
@@ -1680,43 +1687,13 @@ def highlight_min(
16801687
-------
16811688
self : Styler
16821689
"""
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",
16971696
)
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-
)
17201697

17211698
@classmethod
17221699
def from_custom_template(cls, searchpath, name):
@@ -1820,6 +1797,26 @@ def pipe(self, func: Callable, *args, **kwargs):
18201797
return com.pipe(self, func, *args, **kwargs)
18211798

18221799

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+
18231820
class _Tooltips:
18241821
"""
18251822
An extension to ``Styler`` that allows for and manipulates tooltips on hover

0 commit comments

Comments
 (0)