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 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
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 @@ -625,6 +625,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
3 changes: 2 additions & 1 deletion pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ 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,
Copy link
Contributor

Choose a reason for hiding this comment

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

can you format this like

if ((kwds or args) and
    (not isinstance.....)):

Copy link
Contributor Author

@megabyde megabyde Aug 16, 2018

Choose a reason for hiding this comment

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

@jreback The formatting you propose makes Flake complaining:

E129 visually indented line with same indent as next logical line

compat.string_types)):
def f(x):
return func(x, *args, **kwds)
else:
Expand Down
11 changes: 4 additions & 7 deletions pandas/tests/frame/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,10 @@ def test_apply_standard_nonunique(self):
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('kwds', [{}, {'axis': 1}, {'numeric_only': True}])
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add a test for just std that adds both a positional arg (e.g. ddof) and a kwarg (numeric_only) for confirmation here (and other combinations if possible)

def test_with_string_args(self, arg, kwds):
result = self.frame.apply(arg, **kwds)
expected = getattr(self.frame, arg)(**kwds)
tm.assert_series_equal(result, expected)

def test_apply_broadcast_deprecated(self):
Expand Down