Skip to content

BUG: Fixed NDFrame.transform('abs') #20800

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 3 commits into from
Apr 25, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ def get_result(self):

# string dispatch
if isinstance(self.f, compat.string_types):
# Support for `frame.transform('method')`
# Some methods (shift, etc.) require the axis argument, others
# don't, so inspect and insert if nescessary.
func = getattr(self.obj, self.f)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

much better, and now IIRC we actually do something like this in groupby apply (for the same reason). can you add some comments here on what is going on

sig = compat.signature(func)
if 'axis' in sig.args:
Expand Down
10 changes: 7 additions & 3 deletions pandas/tests/frame/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pytest

import operator
from datetime import datetime

import warnings
Expand Down Expand Up @@ -880,11 +881,14 @@ def f():
with np.errstate(all='ignore'):
df.agg({'A': ['abs', 'sum'], 'B': ['mean', 'max']})

def test_transform_abs_name(self):
@pytest.mark.parametrize('method', [
'abs', 'shift', 'pct_change', 'cumsum', 'rank',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could also add ffill and bfill

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Splitting to #20821

])
def test_transform_method_name(self, method):
# https://github.com/pandas-dev/pandas/issues/19760
df = pd.DataFrame({"A": [-1, 2]})
result = df.transform('abs')
expected = pd.DataFrame({"A": [1, 2]})
result = df.transform(method)
expected = operator.methodcaller(method)(df)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Learned something new today. Just out of curiosity, is this any different from doing getattr(df, method)()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in this case, no. methodcaller is nicer though when you need to bind parameters, but don't have the object yet (common in pytest fixtures).

tm.assert_frame_equal(result, expected)

def test_demo(self):
Expand Down