-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: Add Styler.pipe() method (#23229) #23384
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 11 commits
00701c7
c6a92cd
36606ee
73821ba
df6fb42
e5eacbe
51b21ac
a0e758e
21403f7
0363ea8
d1cf095
0798c73
7cbb6b0
6c79509
ae83236
706f3a6
28d7098
726d01d
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 |
---|---|---|
|
@@ -1222,6 +1222,78 @@ class MyStyler(cls): | |
|
||
return MyStyler | ||
|
||
def pipe(self, func, *args, **kwargs): | ||
TomAugspurger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Apply ``func(self, *args, **kwargs)``, and return the result. | ||
TomAugspurger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. versionadded:: 0.24.0 | ||
|
||
Parameters | ||
---------- | ||
func : function | ||
Function to apply to the Styler. | ||
``*args``, and ``**kwargs`` are passed into ``func``. | ||
TomAugspurger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Alternatively a ``(callable, data_keyword)`` tuple where | ||
``data_keyword`` is a string indicating the keyword of | ||
``callable`` that expects the Styler. | ||
TomAugspurger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*args : iterable, optional | ||
Positional arguments passed into ``func``. | ||
**kwargs : dict, optional | ||
Dictionary of keyword arguments passed into ``func``. | ||
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. You can merge args and kwargs in a single line, and you don't need types or optional, as those are always the caseÑ:
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. Changed as suggested. As a result of these changes, the |
||
|
||
Returns | ||
------- | ||
result : object | ||
TomAugspurger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
The value returned by ``func``. | ||
TomAugspurger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
See Also | ||
-------- | ||
DataFrame.pipe : Analogous method for DataFrame. | ||
Styler.apply : Apply a function row-wise, column-wise, or table-wise to | ||
modify the dataframe's styling. | ||
|
||
Notes | ||
TomAugspurger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
----- | ||
Like :meth:`DataFrame.pipe`, this method can simplify the | ||
application of several user-defined functions to a styler. Instead | ||
of writing: | ||
|
||
.. code-block:: python | ||
|
||
f(g(df.style.set_precision(3), arg1=a), arg2=b, arg3=c) | ||
|
||
users can write: | ||
|
||
.. code-block:: python | ||
|
||
(df.style.set_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. | ||
|
||
Examples | ||
-------- | ||
>>> def set_standard_formatting(styler): | ||
... return (styler.set_properties(**{'text-align': 'right'}) | ||
... .format({'X': '{:.1%}'})) | ||
|
||
The user-defined highlight function above can be called within a | ||
sequence of other style modifications: | ||
|
||
>>> df = pd.DataFrame({'A': list(range(-1, 4)), 'X': np.arange(0.2, 1.2, 0.2)}) | ||
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. Use something that looks real, no Then rename 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. I made some changes here, and fixed the reference in the text, which was incorrect. I changed the function name as you suggested, while trying to make it clear that the function was user-defined and application-specific. I'll be honest, using animal names in examples seems a little silly to me, but that's up to the pandas developers. In this case, using numeric data rather than strings made more sense to me, since numeric data presents styling/formatting choices, and special values (min/max/null) can be highlighted. |
||
>>> (df.style | ||
... .set_properties(subset=['X'], **{'background-color': 'yellow'}) | ||
... .pipe(set_standard_formatting) | ||
... .set_caption("Results with column 'X' highlighted.")) | ||
""" | ||
return com._pipe(self, func, *args, **kwargs) | ||
gfyoung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def _is_visible(idx_row, idx_col, lengths): | ||
""" | ||
|
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.
This code is executed. I think the exception at https://travis-ci.org/pandas-dev/pandas/jobs/453316565#L2108 is from this. I'd recommend defining
df
in this code block.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.
Done.