Skip to content

Commit 4aac0c8

Browse files
BUG: Fixed NDFrame.transform('abs') (pandas-dev#20800)
* BUG: Fixed NDFrame.transform('abs') Closes pandas-dev#19760
1 parent 60fe82c commit 4aac0c8

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

pandas/core/apply.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,14 @@ def get_result(self):
111111

112112
# string dispatch
113113
if isinstance(self.f, compat.string_types):
114-
self.kwds['axis'] = self.axis
115-
return getattr(self.obj, self.f)(*self.args, **self.kwds)
114+
# Support for `frame.transform('method')`
115+
# Some methods (shift, etc.) require the axis argument, others
116+
# don't, so inspect and insert if nescessary.
117+
func = getattr(self.obj, self.f)
118+
sig = compat.signature(func)
119+
if 'axis' in sig.args:
120+
self.kwds['axis'] = self.axis
121+
return func(*self.args, **self.kwds)
116122

117123
# ufunc
118124
elif isinstance(self.f, np.ufunc):

pandas/tests/frame/test_apply.py

+11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pytest
66

7+
import operator
78
from datetime import datetime
89

910
import warnings
@@ -880,6 +881,16 @@ def f():
880881
with np.errstate(all='ignore'):
881882
df.agg({'A': ['abs', 'sum'], 'B': ['mean', 'max']})
882883

884+
@pytest.mark.parametrize('method', [
885+
'abs', 'shift', 'pct_change', 'cumsum', 'rank',
886+
])
887+
def test_transform_method_name(self, method):
888+
# https://github.com/pandas-dev/pandas/issues/19760
889+
df = pd.DataFrame({"A": [-1, 2]})
890+
result = df.transform(method)
891+
expected = operator.methodcaller(method)(df)
892+
tm.assert_frame_equal(result, expected)
893+
883894
def test_demo(self):
884895
# demonstration tests
885896
df = pd.DataFrame({'A': range(5), 'B': 5})

0 commit comments

Comments
 (0)