-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: passing str to GroupBy.apply #42021
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
Conversation
jbrockmendel
commented
Jun 15, 2021
- closes #xxxx
- tests added / passed
- Ensure all linting tests pass, see here for how to run them
- whatsnew entry
pandas/core/groupby/groupby.py
Outdated
@@ -1245,6 +1245,16 @@ def f(g): | |||
"func must be a callable if args or kwargs are supplied" | |||
) | |||
else: | |||
if isinstance(func, str): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can this be an elif
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated +greenish
pandas/core/groupby/groupby.py
Outdated
if isinstance(func, str): | ||
if hasattr(self, func): | ||
res = getattr(self, func) | ||
if inspect.isfunction(res) or inspect.ismethod(res): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if callable(res)
?
pandas/core/groupby/groupby.py
Outdated
res = getattr(self, func) | ||
if inspect.isfunction(res) or inspect.ismethod(res): | ||
return res() | ||
return res |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are you returning? does any test actually hit this? should just fall thru no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, several of the test_empty_groupby tests get here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we only raise, is there any issue? I.e. to just do
elif isinstance(func, str) and not hasattr(self, func):
raise TypeError(f"apply func should be callable, not '{func}'")
Or perhaps there is some benefit to adding the call to res()
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you mean return res
unconditionally instead of returning res()
in some cases? that would end up returning obj.method_name
instead of obj.method_name()
, which seems like a pretty weird thing to return from an apply
call
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not quite - I put my suggestion (that doesn't work) below in case you're curious, but see now this PR isn't solely fixing the case of an invalid str argument, but also addressing the # FIXME: apply goes through different code path
. This makes sense now, +1.
if args or kwargs:
if callable(func):
@wraps(func)
def f(g):
with np.errstate(all="ignore"):
return func(g, *args, **kwargs)
elif hasattr(nanops, "nan" + func):
# TODO: should we wrap this in to e.g. _is_builtin_func?
f = getattr(nanops, "nan" + func)
else:
raise ValueError(
"func must be a callable if args or kwargs are supplied"
)
elif isinstance(func, str) and not hasattr(self, func):
raise TypeError(f"apply func should be callable, not '{func}'")
else:
f = func
lgtm, can you merge master one more time. |
Thanks @jbrockmendel |
BUG: passing str to GroupBy.apply (pandas-dev#42021)