-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
DOC: examples for Styler.pipe #45880
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 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3509,41 +3509,85 @@ def pipe(self, func: Callable, *args, **kwargs): | |
|
||
.. code-block:: python | ||
|
||
f(g(df.style.set_precision(3), arg1=a), arg2=b, arg3=c) | ||
f(g(df.style.format(precision=3), arg1=a), arg2=b, arg3=c) | ||
|
||
users can write: | ||
|
||
.. code-block:: python | ||
|
||
(df.style.set_precision(3) | ||
(df.style.format(precision=3) | ||
.pipe(g, arg1=a) | ||
.pipe(f, arg2=b, arg3=c)) | ||
|
||
In particular, this allows users to define functions that take a | ||
styler object, along with other parameters, and return the styler after | ||
making styling changes (such as calling :meth:`Styler.apply` or | ||
:meth:`Styler.set_properties`). Using ``.pipe``, these user-defined | ||
style "transformations" can be interleaved with calls to the built-in | ||
Styler interface. | ||
:meth:`Styler.set_properties`). | ||
|
||
Examples | ||
-------- | ||
>>> def format_conversion(styler): | ||
... return (styler.set_properties(**{'text-align': 'right'}) | ||
... .format({'conversion': '{:.1%}'})) | ||
|
||
The user-defined ``format_conversion`` function above can be called | ||
within a sequence of other style modifications: | ||
|
||
>>> df = pd.DataFrame({'trial': list(range(5)), | ||
... 'conversion': [0.75, 0.85, np.nan, 0.7, 0.72]}) | ||
>>> (df.style | ||
... .highlight_min(subset=['conversion'], color='yellow') | ||
... .pipe(format_conversion) | ||
... .set_caption("Results with minimum conversion highlighted.")) | ||
... # doctest: +SKIP | ||
|
||
.. figure:: ../../_static/style/df_pipe.png | ||
**Common Use** | ||
|
||
The most common usage pattern may be to pre-define styling operations which | ||
can be easily applied to a generic styler in a single ``pipe`` call. | ||
|
||
>>> def some_highlights(styler, min_color="red", max_color="blue"): | ||
... styler.highlight_min(color=min_color, axis=None) | ||
... styler.highlight_max(color=max_color, axis=None) | ||
... styler.highlight_null() | ||
... return styler | ||
>>> df = pd.DataFrame([[1, 2, 3, pd.NA], [pd.NA, 4, 5, 6]], dtype="Int64") | ||
>>> df.style.pipe(some_highlights, min_color="green") # doctest: +SKIP | ||
|
||
.. figure:: ../../_static/style/df_pipe_hl.png | ||
|
||
Since the method returns a ``Styler`` object it can be chained with other | ||
methods as if applying the underlying highlighters directly. | ||
|
||
>>> df.style.format("{:.1f}") | ||
... .pipe(some_highlights, min_color="green") | ||
... .highlight_between(left=2, right=5) # doctest: +SKIP | ||
|
||
.. figure:: ../../_static/style/df_pipe_hl2.png | ||
|
||
**Advanced Use** | ||
|
||
Sometimes it may be necessary to pre-define styling functions, but in the case | ||
where those functions rely on the styler, or data, context. Since | ||
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. Nit: |
||
``Styler.use`` and ``Styler.export`` are designed to be non-data dependent, | ||
they cannot be used for this purpose. Additionally the ``Styler.apply`` | ||
and ``Styler.format`` type methods are not context aware, so a solution | ||
is to use ``pipe`` to dynamically wrap this functionality. | ||
|
||
Suppose we want to code a generic styling function that highlights the final | ||
level of a MultiIndex. The number of levels in the Index is dynamic so we | ||
need the ``Styler`` context to define the level. | ||
|
||
>>> def highlight_last_level(styler): | ||
... return styler.apply_index( | ||
... lambda v: "background-color: pink; color: yellow", axis="columns", | ||
... level=styler.columns.nlevels-1 | ||
... ) # doctest: +SKIP | ||
>>> df.columns = pd.MultiIndex.from_product([["A", "B"], ["X", "Y"]]) | ||
>>> df.style.pipe(highlight_last_level) # doctest: +SKIP | ||
|
||
.. figure:: ../../_static/style/df_pipe_applymap.png | ||
|
||
Additionally suppose we want to highlight a column header if there is any | ||
missing data in that column. | ||
In this case we need the data object itself to determine the effect on the | ||
column headers. | ||
|
||
>>> def highlight_header_missing(styler, level): | ||
... def dynamic_highlight(s): | ||
... return np.where( | ||
... styler.data.isna().any(), "background-color: red;", "" | ||
... ) | ||
... return styler.apply_index(dynamic_highlight, axis=1, level=level) | ||
>>> df.style.pipe(highlight_header_missing, level=1) # doctest: +SKIP | ||
|
||
.. figure:: ../../_static/style/df_pipe_applydata.png | ||
""" | ||
return com.pipe(self, func, *args, **kwargs) | ||
|
||
|
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.
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.
Nit:
The most common usage pattern may be
->A common usage pattern is