-
-
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 2 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,35 @@ 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) | ||
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 should be a little more descriptive. Apply (yes, as reviewers, we can see what the rationale was from your PR, so just put that rationale into the docstring) 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. Okay, I'll change the doctoring, and try to provide some additional context/background. |
||
|
||
Parameters | ||
---------- | ||
func : function | ||
function to apply to the Styler. | ||
``args``, and ``kwargs`` are passed into ``func``. | ||
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 : mapping, optional | ||
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. Add the If you run 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. Done. As mentioned above, now validating. |
||
a dictionary of keyword arguments passed into ``func``. | ||
|
||
Returns | ||
------- | ||
result : the value returned by ``func``. | ||
|
||
See Also | ||
-------- | ||
Styler.apply | ||
Styler.applymap | ||
pandas.DataFrame.pipe | ||
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.
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. Done. |
||
""" | ||
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): | ||
""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1173,6 +1173,25 @@ def test_hide_columns_mult_levels(self): | |
assert ctx['body'][1][2]['is_visible'] | ||
assert ctx['body'][1][2]['display_value'] == 3 | ||
|
||
def test_pipe(self): | ||
def set_caption_from_template(styler, a, b): | ||
return styler.set_caption( | ||
'Dataframe with a = {a} and b = {b}'.format(a=a, b=b)) | ||
styler = self.df.style.pipe(set_caption_from_template, 'A', b='B') | ||
assert 'Dataframe with a = A and b = B' in styler.render() | ||
|
||
def f(s, *args, **kwargs): | ||
return s, args, kwargs | ||
|
||
result = self.df.style.pipe(f, 0, a=1) | ||
assert result[1] == (0,) | ||
assert result[2] == dict(a=1) | ||
|
||
def g(**kwargs): | ||
assert 'styler' in kwargs | ||
return kwargs['styler'].data | ||
assert self.df.style.pipe((g, 'styler')) is self.df | ||
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. Can you comment here explaining why you're using 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. Also, add a comment explaining why you're creating this 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. Yes, I can rewrite this a bit to simplify it. Essentially, I wanted to test that when |
||
|
||
|
||
@td.skip_if_no_mpl | ||
class TestStylerMatplotlibDep(object): | ||
|
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.
Reference the issue number.
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.
Also, you should create a mini-section explaining the enhancement in more detail so that end users can understand the benefit.
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.
Okay, I can do that.