Skip to content

Commit 22653c6

Browse files
committed
merged
1 parent 6716589 commit 22653c6

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed

pandas/io/formats/style.py

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,6 +1711,158 @@ def f(data: FrameOrSeries, props: str) -> np.ndarray:
17111711
f, axis=axis, subset=subset, props=f"background-color: {color};"
17121712
)
17131713

1714+
def highlight_range(
1715+
self,
1716+
subset: Optional[IndexLabel] = None,
1717+
color: str = "yellow",
1718+
start: Optional[Any] = None,
1719+
stop: Optional[Any] = None,
1720+
props: Optional[str] = None,
1721+
) -> Styler:
1722+
"""
1723+
Highlight a defined range by shading the background, or otherwise.
1724+
1725+
Parameters
1726+
----------
1727+
subset : IndexSlice, default None
1728+
A valid slice for ``data`` to limit the style application to.
1729+
color : str, default 'yellow'
1730+
Background color added to CSS style.
1731+
start : scalar or datetime-like, default None
1732+
Left bound for defining the range (inclusive).
1733+
stop : scalar or datetime-like, default None
1734+
Right bound for defining the range (inclusive)
1735+
props : str, default None
1736+
CSS properties to use for highlighting. If ``props`` is given, ``color``
1737+
is not used.
1738+
1739+
Returns
1740+
-------
1741+
self : Styler
1742+
1743+
See Also
1744+
--------
1745+
Styler.highlight_max:
1746+
Styler.highlight_min:
1747+
Styler.highlight_quantile:
1748+
1749+
Notes
1750+
-----
1751+
If ``start`` is ``None`` only the right bound is applied.
1752+
If ``stop`` is ``None`` only the left bound is applied. If both are ``None``
1753+
all values are highlighted.
1754+
1755+
This function only works with compatible ``dtypes``. For example a datetime-like
1756+
region can only use equivalent datetime-like ``start`` and ``stop`` arguments.
1757+
Use ``subset`` to control regions which have multiple ``dtypes``.
1758+
1759+
Examples
1760+
--------
1761+
Using datetimes
1762+
1763+
>>> df = pd.DataFrame({'dates': pd.date_range(start='2021-01-01', periods=10)})
1764+
>>> df.style.highlight_range(start=pd.to_datetime('2021-01-05'))
1765+
1766+
Using ``props`` instead of default background shading
1767+
1768+
>>> df = pd.DataFrame([[1,2], [3,4]])
1769+
>>> df.style.highlight_range(start=2, stop=3, props='font-weight:bold;')
1770+
"""
1771+
1772+
def f(
1773+
data: DataFrame,
1774+
props: str,
1775+
d: Optional[Scalar] = None,
1776+
u: Optional[Scalar] = None,
1777+
) -> np.ndarray:
1778+
ge_d = data >= d if d is not None else np.full_like(data, True, dtype=bool)
1779+
le_u = data <= u if u is not None else np.full_like(data, True, dtype=bool)
1780+
return np.where(ge_d & le_u, props, "")
1781+
1782+
if props is None:
1783+
props = f"background-color: {color};"
1784+
return self.apply(f, axis=None, subset=subset, props=props, d=start, u=stop)
1785+
1786+
def highlight_quantile(
1787+
self,
1788+
subset: Optional[IndexLabel] = None,
1789+
color: str = "yellow",
1790+
q_low: float = 0.0,
1791+
q_high: float = 1.0,
1792+
axis: Optional[Axis] = 0,
1793+
props: Optional[str] = None,
1794+
) -> Styler:
1795+
"""
1796+
Highlight values defined by inclusion in given quantile by shading the
1797+
background, or otherwise.
1798+
1799+
Parameters
1800+
----------
1801+
subset : IndexSlice, default None
1802+
A valid slice for ``data`` to limit the style application to.
1803+
color : str, default 'yellow'
1804+
Background color added to CSS style.
1805+
q_low : float, default 0
1806+
Left bound for the target quantile range (exclusive if not 0).
1807+
q_high : float, default 1
1808+
Right bound for the target quantile range (inclusive)
1809+
axis : {0 or 'index', 1 or 'columns', None}, default 0
1810+
Apply to each column (``axis=0`` or ``'index'``), to each row
1811+
(``axis=1`` or ``'columns'``), or to the entire DataFrame at once
1812+
with ``axis=None``.
1813+
props : str, default None
1814+
CSS properties to use for highlighting. If ``props`` is given, ``color``
1815+
is not used.
1816+
1817+
Returns
1818+
-------
1819+
self : Styler
1820+
1821+
See Also
1822+
--------
1823+
Styler.highlight_max:
1824+
Styler.highlight_min:
1825+
Styler.highlight_range:
1826+
1827+
Notes
1828+
-----
1829+
This function only works with consistent ``dtypes`` within the ``subset``.
1830+
For example a mixture of datetime-like and float items will raise errors.
1831+
1832+
This method uses ``pandas.qcut`` to implement the quantile labelling of data
1833+
values.
1834+
"""
1835+
1836+
def f(
1837+
data: FrameOrSeries,
1838+
props: str,
1839+
q_low: float = 0,
1840+
q_high: float = 1,
1841+
axis_: Optional[Axis] = 0,
1842+
):
1843+
if q_low > 0:
1844+
q, tgt_label = [0, q_low, q_high], 1
1845+
else:
1846+
q, tgt_label = [0, q_high], 0
1847+
if axis_ is None:
1848+
shape = data.values.shape
1849+
labels = pd.qcut(data.values.ravel(), q=q, labels=False).reshape(shape)
1850+
else:
1851+
labels = pd.qcut(data, q=q, labels=False)
1852+
return np.where(labels == tgt_label, props, "")
1853+
1854+
if props is None:
1855+
props = f"background-color: {color};"
1856+
return self.apply(
1857+
f,
1858+
axis=axis,
1859+
subset=subset,
1860+
props=props,
1861+
q_low=q_low,
1862+
q_high=q_high,
1863+
axis_=axis,
1864+
)
1865+
17141866
@classmethod
17151867
def from_custom_template(cls, searchpath, name):
17161868
"""

0 commit comments

Comments
 (0)