-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: Styler builtins: highlight_between
#39821
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 52 commits
Commits
Show all changes
69 commits
Select commit
Hold shift + click to select a range
22653c6
merged
attack68 6c73f62
merged
attack68 fe08398
upodate docs and consistency
attack68 aaa15bc
upodate docs and consistency
attack68 a362fbf
add and update tests
attack68 6ab6dca
highlight range now accepts sequence
attack68 411ef6b
highlight range now accepts sequence
attack68 85e5241
highlight range now accepts sequence
attack68 4f105d6
examples
attack68 26f6d12
reorder
attack68 e7d9d19
Merge remote-tracking branch 'upstream/master' into range_builtin
attack68 92eb5fd
adj tests
attack68 cbea15a
adj tests
attack68 caef3a5
whats new
attack68 dc12b0c
to_numpy()
attack68 95c1a68
docstring fix
attack68 d965635
test fix
attack68 b30b099
improve coding remove conditional
attack68 d947c00
docs links
attack68 f248797
Merge remote-tracking branch 'upstream/master' into range_builtin
attack68 5ffe49f
add tests to new modules.
attack68 6b5bc67
add examples and input validation
attack68 c9d70ed
add examples and input validation
attack68 8baf65f
Merge remote-tracking branch 'upstream/master' into range_builtin
attack68 2971556
Merge remote-tracking branch 'upstream/master' into range_builtin
attack68 869154e
Merge remote-tracking branch 'upstream/master' into range_builtin
attack68 f409884
Merge remote-tracking branch 'upstream/master' into range_builtin
attack68 bc1eb6b
Merge remote-tracking branch 'upstream/master' into range_builtin
attack68 e086fc7
Merge remote-tracking branch 'upstream/master' into range_builtin
attack68 1aefc6b
name change: highlight_range to highlight_between
attack68 7f8d335
split PR
attack68 e4abcbe
split PR
attack68 3522160
split PR
attack68 ed4a758
split PR
attack68 5e982c7
split PR
attack68 cc4148b
split PR
attack68 41c1c38
split PR
attack68 a36bc5b
Merge remote-tracking branch 'upstream/master' into range_builtin
attack68 5fc8124
Merge remote-tracking branch 'upstream/master' into range_builtin
attack68 0bd7323
remove reference to highlight_quantile
attack68 b14504d
Merge remote-tracking branch 'upstream/master' into range_builtin
attack68 7c7a337
link see alsos
attack68 f80d012
doc edit
attack68 0228ab7
doc edit
attack68 2a81ea1
Merge branch 'master' into range_builtin
attack68 472d321
remove bool option from inclusive
attack68 d03b905
Merge branch 'master' into range_builtin
attack68 37b131f
use operator
attack68 3533763
remove misleading random note from test fixtures
attack68 c69f29d
remove misleading random note from test fixtures
attack68 7731978
Merge remote-tracking branch 'upstream/master' into range_builtin
attack68 9168be7
mypy fix
attack68 0c92a50
Merge remote-tracking branch 'upstream/master' into range_builtin
attack68 86fb87c
filesnames
attack68 53bcc1b
Merge remote-tracking branch 'upstream/master' into range_builtin
attack68 bbb63d4
add arg shape validation function
attack68 6f59dbf
Merge remote-tracking branch 'upstream/master' into range_builtin
attack68 6dd8137
mypy updates
attack68 8bf57aa
Merge remote-tracking branch 'upstream/master' into range_builtin
attack68 e9a4840
Merge remote-tracking branch 'upstream/master' into range_builtin
attack68 4a575d4
Merge remote-tracking branch 'upstream/master' into range_builtin
attack68 f6858d1
Merge remote-tracking branch 'upstream/master' into range_builtin
attack68 45be30a
integrate into new test structure
attack68 dcc651d
Merge remote-tracking branch 'upstream/master' into range_builtin
attack68 c779961
remove axis from validate arg func
attack68 adf607d
Merge remote-tracking branch 'upstream/master' into range_builtin
attack68 210a00d
no bool in inclusive
attack68 73861f6
revert random explicit
attack68 3c57373
Merge remote-tracking branch 'upstream/master' into range_builtin
attack68 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
from contextlib import contextmanager | ||
import copy | ||
from functools import partial | ||
import operator | ||
from typing import ( | ||
Any, | ||
Callable, | ||
|
@@ -33,6 +34,7 @@ | |
FrameOrSeries, | ||
FrameOrSeriesUnion, | ||
IndexLabel, | ||
Scalar, | ||
) | ||
from pandas.compat._optional import import_optional_dependency | ||
from pandas.util._decorators import doc | ||
|
@@ -1701,6 +1703,7 @@ def highlight_null( | |
-------- | ||
Styler.highlight_max: Highlight the maximum with a style. | ||
Styler.highlight_min: Highlight the minimum with a style. | ||
Styler.highlight_between: Highlight a defined range with a style. | ||
""" | ||
|
||
def f(data: DataFrame, props: str) -> np.ndarray: | ||
|
@@ -1748,6 +1751,7 @@ def highlight_max( | |
-------- | ||
Styler.highlight_null: Highlight missing values with a style. | ||
Styler.highlight_min: Highlight the minimum with a style. | ||
Styler.highlight_between: Highlight a defined range with a style. | ||
""" | ||
|
||
def f(data: FrameOrSeries, props: str) -> np.ndarray: | ||
|
@@ -1795,6 +1799,7 @@ def highlight_min( | |
-------- | ||
Styler.highlight_null: Highlight missing values with a style. | ||
Styler.highlight_max: Highlight the maximum with a style. | ||
Styler.highlight_between: Highlight a defined range with a style. | ||
""" | ||
|
||
def f(data: FrameOrSeries, props: str) -> np.ndarray: | ||
|
@@ -1808,6 +1813,163 @@ def f(data: FrameOrSeries, props: str) -> np.ndarray: | |
f, axis=axis, subset=subset, props=props # type: ignore[arg-type] | ||
) | ||
|
||
def highlight_between( | ||
self, | ||
subset: Optional[IndexLabel] = None, | ||
color: str = "yellow", | ||
axis: Optional[Axis] = 0, | ||
left: Optional[Union[Scalar, Sequence]] = None, | ||
right: Optional[Union[Scalar, Sequence]] = None, | ||
inclusive: Union[bool, str] = "both", | ||
props: Optional[str] = None, | ||
) -> Styler: | ||
""" | ||
Highlight a defined range with a style. | ||
|
||
.. versionadded:: 1.3.0 | ||
|
||
Parameters | ||
---------- | ||
subset : IndexSlice, default None | ||
A valid slice for ``data`` to limit the style application to. | ||
color : str, default 'yellow' | ||
Background color to use for highlighting. | ||
axis : {0 or 'index', 1 or 'columns', None}, default 0 | ||
If ``left`` or ``right`` given as sequence, axis along which to apply those | ||
boundaries. See examples. | ||
left : scalar or datetime-like, or sequence or array-like, default None | ||
Left bound for defining the range. | ||
right : scalar or datetime-like, or sequence or array-like, default None | ||
Right bound for defining the range. | ||
inclusive : {'both', 'neither', 'left', 'right'} | ||
Identify whether bounds are closed or open. | ||
props : str, default None | ||
CSS properties to use for highlighting. If ``props`` is given, ``color`` | ||
is not used. | ||
|
||
Returns | ||
------- | ||
self : Styler | ||
|
||
See Also | ||
-------- | ||
Styler.highlight_null: Highlight missing values with a style. | ||
Styler.highlight_max: Highlight the maximum with a style. | ||
Styler.highlight_min: Highlight the minimum with a style. | ||
|
||
Notes | ||
----- | ||
If ``left`` is ``None`` only the right bound is applied. | ||
If ``right`` is ``None`` only the left bound is applied. If both are ``None`` | ||
all values are highlighted. | ||
|
||
``axis`` is only needed if ``left`` or ``right`` are provided as a sequence or | ||
an array-like object for aligning the shapes. If ``left`` and ``right`` are | ||
both scalars then all ``axis`` inputs will give the same result. | ||
|
||
This function only works with compatible ``dtypes``. For example a datetime-like | ||
region can only use equivalent datetime-like ``left`` and ``right`` arguments. | ||
Use ``subset`` to control regions which have multiple ``dtypes``. | ||
|
||
Examples | ||
-------- | ||
Basic usage | ||
|
||
>>> df = pd.DataFrame({ | ||
... 'One': [1.2, 1.6, 1.5], | ||
... 'Two': [2.9, 2.1, 2.5], | ||
... 'Three': [3.1, 3.2, 3.8], | ||
... }) | ||
>>> df.style.highlight_between(left=2.1, right=2.9) | ||
|
||
.. figure:: ../../_static/style/hr_basic.png | ||
|
||
Using a range input sequnce along an ``axis``, in this case setting a ``left`` | ||
and ``right`` for each column individually | ||
|
||
>>> df.style.highlight_between(left=[1.4, 2.4, 3.4], right=[1.6, 2.6, 3.6], | ||
... axis=1, color="#fffd75") | ||
|
||
.. figure:: ../../_static/style/hr_seq.png | ||
|
||
Using ``axis=None`` and providing the ``left`` argument as an array that | ||
matches the input DataFrame, with a constant ``right`` | ||
|
||
>>> df.style.highlight_between(left=[[2,2,3],[2,2,3],[3,3,3]], right=3.5, | ||
... axis=None, color="#fffd75") | ||
|
||
.. figure:: ../../_static/style/hr_axNone.png | ||
|
||
Using ``props`` instead of default background coloring | ||
|
||
>>> df.style.highlight_between(left=1.5, right=3.5, | ||
... props='font-weight:bold;color:#e83e8c') | ||
|
||
.. figure:: ../../_static/style/hr_props.png | ||
""" | ||
|
||
def f( | ||
data: FrameOrSeries, | ||
props: str, | ||
left: Optional[Union[Scalar, Sequence]] = None, | ||
right: Optional[Union[Scalar, Sequence]] = None, | ||
inclusive: Union[bool, str] = True, | ||
) -> np.ndarray: | ||
def reshape(x, arg): | ||
"""if x is sequence check it fits axis, otherwise raise""" | ||
if np.iterable(x) and not isinstance(x, str): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is not alignment. use .align() There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. align was bad name in code. |
||
try: | ||
return np.asarray(x).reshape(data.shape) | ||
except ValueError: | ||
raise ValueError( | ||
f"supplied '{arg}' is not correct shape for " | ||
"data over selected 'axis': got " | ||
f"{np.asarray(x).shape}, expected " | ||
f"{data.shape}" | ||
) | ||
return x | ||
|
||
left, right = reshape(left, "left"), reshape(right, "right") | ||
|
||
# get ops with correct boundary attribution | ||
if inclusive == "both": | ||
ops = (operator.ge, operator.le) | ||
elif inclusive == "neither": | ||
ops = (operator.gt, operator.lt) | ||
elif inclusive == "left": | ||
ops = (operator.ge, operator.lt) | ||
elif inclusive == "right": | ||
ops = (operator.gt, operator.le) | ||
else: | ||
raise ValueError( | ||
f"'inclusive' values can be 'both', 'left', 'right', or 'neither' " | ||
f"got {inclusive}" | ||
) | ||
|
||
g_left = ( | ||
ops[0](data, left) | ||
if left is not None | ||
else np.full(data.shape, True, dtype=bool) | ||
) | ||
l_right = ( | ||
ops[1](data, right) | ||
if right is not None | ||
else np.full(data.shape, True, dtype=bool) | ||
) | ||
return np.where(g_left & l_right, props, "") | ||
|
||
if props is None: | ||
props = f"background-color: {color};" | ||
return self.apply( | ||
f, # type: ignore[arg-type] | ||
axis=axis, | ||
subset=subset, | ||
props=props, | ||
left=left, | ||
right=right, | ||
inclusive=inclusive, | ||
) | ||
|
||
@classmethod | ||
def from_custom_template(cls, searchpath, name): | ||
""" | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.