-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: consistent Styler.highlight_X
arg signature with props
#40242
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
Changes from 4 commits
fa9c8d5
c4d799d
cbb16d9
1829009
e131b93
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -941,7 +941,7 @@ def apply( | |
Examples | ||
-------- | ||
>>> def highlight_max(x, color): | ||
... return np.where(x == np.nanmax(x.values), f"color: {color};", None) | ||
... return np.where(x == np.nanmax(x.to_numpy()), f"color: {color};", None) | ||
>>> df = pd.DataFrame(np.random.randn(5, 2)) | ||
>>> df.style.apply(highlight_max, color='red') | ||
>>> df.style.apply(highlight_max, color='blue', axis=1) | ||
|
@@ -1636,9 +1636,10 @@ def highlight_null( | |
self, | ||
null_color: str = "red", | ||
subset: Optional[IndexLabel] = None, | ||
props: Optional[str] = None, | ||
) -> Styler: | ||
""" | ||
Shade the background ``null_color`` for missing values. | ||
Highlight missing values with a style. | ||
|
||
Parameters | ||
---------- | ||
|
@@ -1648,79 +1649,124 @@ def highlight_null( | |
|
||
.. versionadded:: 1.1.0 | ||
|
||
props : str, default None | ||
CSS properties to use for highlighting. If ``props`` is given, ``color`` | ||
is not used. | ||
|
||
.. versionadded:: 1.3.0 | ||
|
||
Returns | ||
------- | ||
self : Styler | ||
|
||
See Also | ||
-------- | ||
Styler.highlight_max: Highlight the maximum with a style. | ||
Styler.highlight_min: Highlight the minimum with a style. | ||
Styler.highlight_quantile: Highlight values defined by a quantile with a style. | ||
Styler.highlight_between: Highlight a defined range with a style. | ||
|
||
Notes | ||
----- | ||
Uses ``pandas.isna()`` to detect the missing values. | ||
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. why is this here? we use isna for detecting missing values by default, so this is not surprising. 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. removed |
||
""" | ||
|
||
def f(data: DataFrame, props: str) -> np.ndarray: | ||
return np.where(pd.isna(data).values, props, "") | ||
return np.where(pd.isna(data).to_numpy(), props, "") | ||
|
||
return self.apply( | ||
f, axis=None, subset=subset, props=f"background-color: {null_color};" | ||
) | ||
if props is None: | ||
props = f"background-color: {null_color};" | ||
return self.apply(f, axis=None, subset=subset, props=props) | ||
|
||
def highlight_max( | ||
self, | ||
subset: Optional[IndexLabel] = None, | ||
color: str = "yellow", | ||
axis: Optional[Axis] = 0, | ||
props: Optional[str] = None, | ||
) -> Styler: | ||
""" | ||
Highlight the maximum by shading the background. | ||
Highlight the maximum with a style. | ||
|
||
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 | ||
Apply to each column (``axis=0`` or ``'index'``), to each row | ||
(``axis=1`` or ``'columns'``), or to the entire DataFrame at once | ||
with ``axis=None``. | ||
props : str, default None | ||
CSS properties to use for highlighting. If ``props`` is given, ``color`` | ||
is not used. | ||
|
||
.. versionadded:: 1.3.0 | ||
|
||
Returns | ||
------- | ||
self : Styler | ||
|
||
See Also | ||
-------- | ||
Styler.highlight_null: Highlight missing values with a style. | ||
Styler.highlight_min: Highlight the minimum with a style. | ||
Styler.highlight_quantile: Highlight values defined by a quantile with a style. | ||
Styler.highlight_between: Highlight a defined range with a style. | ||
""" | ||
|
||
def f(data: FrameOrSeries, props: str) -> np.ndarray: | ||
return np.where(data == np.nanmax(data.values), props, "") | ||
return np.where(data == np.nanmax(data.to_numpy()), props, "") | ||
|
||
return self.apply( | ||
f, axis=axis, subset=subset, props=f"background-color: {color};" | ||
) | ||
if props is None: | ||
props = f"background-color: {color};" | ||
return self.apply(f, axis=axis, subset=subset, props=props) | ||
|
||
def highlight_min( | ||
self, | ||
subset: Optional[IndexLabel] = None, | ||
color: str = "yellow", | ||
axis: Optional[Axis] = 0, | ||
props: Optional[str] = None, | ||
) -> Styler: | ||
""" | ||
Highlight the minimum by shading the background. | ||
Highlight the minimum with a style. | ||
|
||
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 | ||
Apply to each column (``axis=0`` or ``'index'``), to each row | ||
(``axis=1`` or ``'columns'``), or to the entire DataFrame at once | ||
with ``axis=None``. | ||
props : str, default None | ||
CSS properties to use for highlighting. If ``props`` is given, ``color`` | ||
is not used. | ||
|
||
.. versionadded:: 1.3.0 | ||
|
||
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_quantile: Highlight values defined by a quantile with a style. | ||
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. same 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. removed |
||
Styler.highlight_between: Highlight a defined range with a style. | ||
""" | ||
|
||
def f(data: FrameOrSeries, props: str) -> np.ndarray: | ||
return np.where(data == np.nanmin(data.values), props, "") | ||
return np.where(data == np.nanmin(data.to_numpy()), props, "") | ||
|
||
return self.apply( | ||
f, axis=axis, subset=subset, props=f"background-color: {color};" | ||
) | ||
if props is None: | ||
props = f"background-color: {color};" | ||
return self.apply(f, axis=axis, subset=subset, props=props) | ||
|
||
@classmethod | ||
def from_custom_template(cls, searchpath, name): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need to take these new highlites out
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed