|
27 | 27 | from pandas._config import get_option
|
28 | 28 |
|
29 | 29 | from pandas._libs import lib
|
30 |
| -from pandas._typing import Axis, FrameOrSeries, FrameOrSeriesUnion, IndexLabel |
| 30 | +from pandas._typing import Axis, FrameOrSeries, FrameOrSeriesUnion, IndexLabel, Scalar |
31 | 31 | from pandas.compat._optional import import_optional_dependency
|
32 | 32 | from pandas.util._decorators import doc
|
33 | 33 |
|
@@ -1758,16 +1758,26 @@ def highlight_range(
|
1758 | 1758 | >>> df = pd.DataFrame([[1,2], [3,4]])
|
1759 | 1759 | >>> df.style.highlight_range(start=2, stop=3, props='font-weight:bold;')
|
1760 | 1760 | """
|
| 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 | + |
1761 | 1772 | if props is None:
|
1762 | 1773 | props = f"background-color: {color};"
|
1763 | 1774 | return self.apply(
|
1764 |
| - _Builtins._highlight_func, |
| 1775 | + f, |
1765 | 1776 | axis=None,
|
1766 | 1777 | subset=subset,
|
1767 | 1778 | props=props,
|
1768 |
| - highlight="range", |
1769 |
| - start=start, |
1770 |
| - stop=stop, |
| 1779 | + d=start, |
| 1780 | + u=stop, |
1771 | 1781 | )
|
1772 | 1782 |
|
1773 | 1783 | def highlight_quantile(
|
@@ -1819,14 +1829,32 @@ def highlight_quantile(
|
1819 | 1829 | This method uses ``pandas.qcut`` to implement the quantile labelling of data
|
1820 | 1830 | values.
|
1821 | 1831 | """
|
| 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 | + |
1822 | 1851 | if props is None:
|
1823 | 1852 | props = f"background-color: {color};"
|
1824 | 1853 | return self.apply(
|
1825 |
| - _Builtins._highlight_func, |
| 1854 | + f, |
1826 | 1855 | axis=axis,
|
1827 | 1856 | subset=subset,
|
1828 | 1857 | props=props,
|
1829 |
| - highlight="quantile", |
1830 | 1858 | q_low=q_low,
|
1831 | 1859 | q_high=q_high,
|
1832 | 1860 | axis_=axis,
|
@@ -1934,46 +1962,6 @@ def pipe(self, func: Callable, *args, **kwargs):
|
1934 | 1962 | return com.pipe(self, func, *args, **kwargs)
|
1935 | 1963 |
|
1936 | 1964 |
|
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 |
| - |
1977 | 1965 | class _Tooltips:
|
1978 | 1966 | """
|
1979 | 1967 | An extension to ``Styler`` that allows for and manipulates tooltips on hover
|
|
0 commit comments