Skip to content

Commit 6d3a1dc

Browse files
committed
simplify
1 parent 4a2052e commit 6d3a1dc

File tree

1 file changed

+35
-47
lines changed

1 file changed

+35
-47
lines changed

pandas/io/formats/style.py

Lines changed: 35 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from pandas._config import get_option
2828

2929
from pandas._libs import lib
30-
from pandas._typing import Axis, FrameOrSeries, FrameOrSeriesUnion, IndexLabel
30+
from pandas._typing import Axis, FrameOrSeries, FrameOrSeriesUnion, IndexLabel, Scalar
3131
from pandas.compat._optional import import_optional_dependency
3232
from pandas.util._decorators import doc
3333

@@ -1758,16 +1758,26 @@ def highlight_range(
17581758
>>> df = pd.DataFrame([[1,2], [3,4]])
17591759
>>> df.style.highlight_range(start=2, stop=3, props='font-weight:bold;')
17601760
"""
1761+
1762+
def f(
1763+
data: DataFrame,
1764+
props: str,
1765+
d: Optional[Scalar] = None,
1766+
u: Optional[Scalar] = None,
1767+
) -> np.ndarray:
1768+
ge_d = data >= d if d is not None else np.full_like(data, True, dtype=bool)
1769+
le_u = data <= u if u is not None else np.full_like(data, True, dtype=bool)
1770+
return np.where(ge_d & le_u, props, "")
1771+
17611772
if props is None:
17621773
props = f"background-color: {color};"
17631774
return self.apply(
1764-
_Builtins._highlight_func,
1775+
f,
17651776
axis=None,
17661777
subset=subset,
17671778
props=props,
1768-
highlight="range",
1769-
start=start,
1770-
stop=stop,
1779+
d=start,
1780+
u=stop,
17711781
)
17721782

17731783
def highlight_quantile(
@@ -1819,14 +1829,32 @@ def highlight_quantile(
18191829
This method uses ``pandas.qcut`` to implement the quantile labelling of data
18201830
values.
18211831
"""
1832+
1833+
def f(
1834+
data: FrameOrSeries,
1835+
props: str,
1836+
q_low: float = 0,
1837+
q_high: float = 1,
1838+
axis_: Optional[Axis] = 0,
1839+
):
1840+
if q_low > 0:
1841+
q, tgt_label = [0, q_low, q_high], 1
1842+
else:
1843+
q, tgt_label = [0, q_high], 0
1844+
if axis_ is None:
1845+
shape = data.values.shape
1846+
labels = pd.qcut(data.values.ravel(), q=q, labels=False).reshape(shape)
1847+
else:
1848+
labels = pd.qcut(data, q=q, labels=False)
1849+
return np.where(labels == tgt_label, props, "")
1850+
18221851
if props is None:
18231852
props = f"background-color: {color};"
18241853
return self.apply(
1825-
_Builtins._highlight_func,
1854+
f,
18261855
axis=axis,
18271856
subset=subset,
18281857
props=props,
1829-
highlight="quantile",
18301858
q_low=q_low,
18311859
q_high=q_high,
18321860
axis_=axis,
@@ -1934,46 +1962,6 @@ def pipe(self, func: Callable, *args, **kwargs):
19341962
return com.pipe(self, func, *args, **kwargs)
19351963

19361964

1937-
class _Builtins:
1938-
@staticmethod
1939-
def _highlight_func(
1940-
data: FrameOrSeries,
1941-
props: str = "background-color: yellow;",
1942-
highlight: str = "max",
1943-
**kwargs,
1944-
) -> np.ndarray:
1945-
"""
1946-
Highlight the value in a Series or DataFrame by func with css-properties
1947-
"""
1948-
if highlight == "max":
1949-
return np.where(data == np.nanmax(data.values), props, "")
1950-
elif highlight == "min":
1951-
return np.where(data == np.nanmin(data.values), props, "")
1952-
elif highlight == "null":
1953-
return np.where(pd.isna(data).values, props, "")
1954-
elif highlight == "range":
1955-
l, h = kwargs.get("start"), kwargs.get("stop")
1956-
ge_l = data >= l if l is not None else np.full_like(data, True, dtype=bool)
1957-
le_h = data <= h if h is not None else np.full_like(data, True, dtype=bool)
1958-
return np.where(ge_l & le_h, props, "")
1959-
elif highlight == "quantile":
1960-
l, h = kwargs.get("q_low"), kwargs.get("q_high")
1961-
if l > 0:
1962-
q, tgt_label = [0, l, h], 1
1963-
else:
1964-
q, tgt_label = [0, h], 0
1965-
if kwargs.get("axis_") is None:
1966-
shape = data.values.shape
1967-
labels = pd.qcut(data.values.ravel(), q=q, labels=False).reshape(shape)
1968-
else:
1969-
labels = pd.qcut(data, q=q, labels=False)
1970-
return np.where(labels == tgt_label, props, "")
1971-
else:
1972-
raise ValueError(
1973-
"'highlight' must be one of 'max', 'min', 'null', 'range', 'quantile'."
1974-
)
1975-
1976-
19771965
class _Tooltips:
19781966
"""
19791967
An extension to ``Styler`` that allows for and manipulates tooltips on hover

0 commit comments

Comments
 (0)