Skip to content

BUG: Fix DataFrame.apply for string arg with additional args (#22376) #22377

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 5 commits into from
Aug 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ Numeric
a ``TypeError`` was wrongly raised. For all three methods such calculation are now done correctly. (:issue:`16679`).
- Bug in :class:`Series` comparison against datetime-like scalars and arrays (:issue:`22074`)
- Bug in :class:`DataFrame` multiplication between boolean dtype and integer returning ``object`` dtype instead of integer dtype (:issue:`22047`,:issue:`22163`)
- 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`)
-

Strings
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ def __init__(self, obj, func, broadcast, raw, reduce, result_type,
self.result_type = result_type

# curry if needed
if kwds or args and not isinstance(func, np.ufunc):
if ((kwds or args) and
not isinstance(func, (np.ufunc, compat.string_types))):

def f(x):
return func(x, *args, **kwds)
else:
Expand Down
19 changes: 11 additions & 8 deletions pandas/tests/frame/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,17 @@ def test_apply_standard_nonunique(self):
rs = df.T.apply(lambda s: s[0], axis=0)
assert_series_equal(rs, xp)

@pytest.mark.parametrize('arg', ['sum', 'mean', 'min', 'max', 'std'])
def test_with_string_args(self, arg):
result = self.frame.apply(arg)
expected = getattr(self.frame, arg)()
tm.assert_series_equal(result, expected)

result = self.frame.apply(arg, axis=1)
expected = getattr(self.frame, arg)(axis=1)
@pytest.mark.parametrize('func', ['sum', 'mean', 'min', 'max', 'std'])
@pytest.mark.parametrize('args,kwds', [
pytest.param([], {}, id='no_args_or_kwds'),
pytest.param([1], {}, id='axis_from_args'),
pytest.param([], {'axis': 1}, id='axis_from_kwds'),
pytest.param([], {'numeric_only': True}, id='optional_kwds'),
pytest.param([1, None], {'numeric_only': True}, id='args_and_kwds')
])
def test_apply_with_string_funcs(self, func, args, kwds):
result = self.frame.apply(func, *args, **kwds)
expected = getattr(self.frame, func)(*args, **kwds)
tm.assert_series_equal(result, expected)

def test_apply_broadcast_deprecated(self):
Expand Down