Skip to content

Commit 1708851

Browse files
megabydevictor
authored and
victor
committed
BUG: Fix DataFrame.apply for string arg with additional args (pandas-dev#22376) (pandas-dev#22377)
1 parent e4718c7 commit 1708851

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,7 @@ Numeric
634634
a ``TypeError`` was wrongly raised. For all three methods such calculation are now done correctly. (:issue:`16679`).
635635
- Bug in :class:`Series` comparison against datetime-like scalars and arrays (:issue:`22074`)
636636
- Bug in :class:`DataFrame` multiplication between boolean dtype and integer returning ``object`` dtype instead of integer dtype (:issue:`22047`,:issue:`22163`)
637+
- Bug in :meth:`DataFrame.apply` where, when supplied with a string argument and additional positional or keyword arguments (e.g. ``df.apply('sum', min_count=1)``), a ``TypeError`` was wrongly raised (:issue:`22376`)
637638
-
638639

639640
Strings

pandas/core/apply.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ def __init__(self, obj, func, broadcast, raw, reduce, result_type,
7171
self.result_type = result_type
7272

7373
# curry if needed
74-
if kwds or args and not isinstance(func, np.ufunc):
74+
if ((kwds or args) and
75+
not isinstance(func, (np.ufunc, compat.string_types))):
76+
7577
def f(x):
7678
return func(x, *args, **kwds)
7779
else:

pandas/tests/frame/test_apply.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,17 @@ def test_apply_standard_nonunique(self):
120120
rs = df.T.apply(lambda s: s[0], axis=0)
121121
assert_series_equal(rs, xp)
122122

123-
@pytest.mark.parametrize('arg', ['sum', 'mean', 'min', 'max', 'std'])
124-
def test_with_string_args(self, arg):
125-
result = self.frame.apply(arg)
126-
expected = getattr(self.frame, arg)()
127-
tm.assert_series_equal(result, expected)
128-
129-
result = self.frame.apply(arg, axis=1)
130-
expected = getattr(self.frame, arg)(axis=1)
123+
@pytest.mark.parametrize('func', ['sum', 'mean', 'min', 'max', 'std'])
124+
@pytest.mark.parametrize('args,kwds', [
125+
pytest.param([], {}, id='no_args_or_kwds'),
126+
pytest.param([1], {}, id='axis_from_args'),
127+
pytest.param([], {'axis': 1}, id='axis_from_kwds'),
128+
pytest.param([], {'numeric_only': True}, id='optional_kwds'),
129+
pytest.param([1, None], {'numeric_only': True}, id='args_and_kwds')
130+
])
131+
def test_apply_with_string_funcs(self, func, args, kwds):
132+
result = self.frame.apply(func, *args, **kwds)
133+
expected = getattr(self.frame, func)(*args, **kwds)
131134
tm.assert_series_equal(result, expected)
132135

133136
def test_apply_broadcast_deprecated(self):

0 commit comments

Comments
 (0)