Skip to content

Commit 6967785

Browse files
authored
restructure highlight_between (#40928)
Co-authored-by: JHM Darbyshire (iMac) <[email protected]>
1 parent 8d34106 commit 6967785

File tree

1 file changed

+49
-46
lines changed

1 file changed

+49
-46
lines changed

pandas/io/formats/style.py

+49-46
Original file line numberDiff line numberDiff line change
@@ -1558,55 +1558,10 @@ def highlight_between(
15581558
15591559
.. figure:: ../../_static/style/hbetw_props.png
15601560
"""
1561-
1562-
def f(
1563-
data: FrameOrSeries,
1564-
props: str,
1565-
left: Scalar | Sequence | np.ndarray | FrameOrSeries | None = None,
1566-
right: Scalar | Sequence | np.ndarray | FrameOrSeries | None = None,
1567-
inclusive: bool | str = True,
1568-
) -> np.ndarray:
1569-
if np.iterable(left) and not isinstance(left, str):
1570-
left = _validate_apply_axis_arg(
1571-
left, "left", None, data # type: ignore[arg-type]
1572-
)
1573-
1574-
if np.iterable(right) and not isinstance(right, str):
1575-
right = _validate_apply_axis_arg(
1576-
right, "right", None, data # type: ignore[arg-type]
1577-
)
1578-
1579-
# get ops with correct boundary attribution
1580-
if inclusive == "both":
1581-
ops = (operator.ge, operator.le)
1582-
elif inclusive == "neither":
1583-
ops = (operator.gt, operator.lt)
1584-
elif inclusive == "left":
1585-
ops = (operator.ge, operator.lt)
1586-
elif inclusive == "right":
1587-
ops = (operator.gt, operator.le)
1588-
else:
1589-
raise ValueError(
1590-
f"'inclusive' values can be 'both', 'left', 'right', or 'neither' "
1591-
f"got {inclusive}"
1592-
)
1593-
1594-
g_left = (
1595-
ops[0](data, left)
1596-
if left is not None
1597-
else np.full(data.shape, True, dtype=bool)
1598-
)
1599-
l_right = (
1600-
ops[1](data, right)
1601-
if right is not None
1602-
else np.full(data.shape, True, dtype=bool)
1603-
)
1604-
return np.where(g_left & l_right, props, "")
1605-
16061561
if props is None:
16071562
props = f"background-color: {color};"
16081563
return self.apply(
1609-
f, # type: ignore[arg-type]
1564+
_highlight_between, # type: ignore[arg-type]
16101565
axis=axis,
16111566
subset=subset,
16121567
props=props,
@@ -1831,3 +1786,51 @@ def css(rgba) -> str:
18311786
index=data.index,
18321787
columns=data.columns,
18331788
)
1789+
1790+
1791+
def _highlight_between(
1792+
data: FrameOrSeries,
1793+
props: str,
1794+
left: Scalar | Sequence | np.ndarray | FrameOrSeries | None = None,
1795+
right: Scalar | Sequence | np.ndarray | FrameOrSeries | None = None,
1796+
inclusive: bool | str = True,
1797+
) -> np.ndarray:
1798+
"""
1799+
Return an array of css props based on condition of data values within given range.
1800+
"""
1801+
if np.iterable(left) and not isinstance(left, str):
1802+
left = _validate_apply_axis_arg(
1803+
left, "left", None, data # type: ignore[arg-type]
1804+
)
1805+
1806+
if np.iterable(right) and not isinstance(right, str):
1807+
right = _validate_apply_axis_arg(
1808+
right, "right", None, data # type: ignore[arg-type]
1809+
)
1810+
1811+
# get ops with correct boundary attribution
1812+
if inclusive == "both":
1813+
ops = (operator.ge, operator.le)
1814+
elif inclusive == "neither":
1815+
ops = (operator.gt, operator.lt)
1816+
elif inclusive == "left":
1817+
ops = (operator.ge, operator.lt)
1818+
elif inclusive == "right":
1819+
ops = (operator.gt, operator.le)
1820+
else:
1821+
raise ValueError(
1822+
f"'inclusive' values can be 'both', 'left', 'right', or 'neither' "
1823+
f"got {inclusive}"
1824+
)
1825+
1826+
g_left = (
1827+
ops[0](data, left)
1828+
if left is not None
1829+
else np.full(data.shape, True, dtype=bool)
1830+
)
1831+
l_right = (
1832+
ops[1](data, right)
1833+
if right is not None
1834+
else np.full(data.shape, True, dtype=bool)
1835+
)
1836+
return np.where(g_left & l_right, props, "")

0 commit comments

Comments
 (0)