The Wayback Machine - https://web.archive.org/web/20220221170412/https://github.com/pandas-dev/pandas/pull/42021
Skip to content
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

BUG: passing str to GroupBy.apply #42021

Merged
merged 17 commits into from Jul 17, 2021
Merged

Conversation

@jbrockmendel
Copy link
Member

@jbrockmendel jbrockmendel commented Jun 15, 2021

  • closes #xxxx
  • tests added / passed
  • Ensure all linting tests pass, see here for how to run them
  • whatsnew entry
@@ -1245,6 +1245,16 @@ def f(g):
"func must be a callable if args or kwargs are supplied"
)
else:
if isinstance(func, str):
Copy link
Contributor

@jreback jreback Jun 15, 2021

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

Copy link
Member Author

@jbrockmendel jbrockmendel Jun 16, 2021

Choose a reason for hiding this comment

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

Updated +greenish

if isinstance(func, str):
if hasattr(self, func):
res = getattr(self, func)
if inspect.isfunction(res) or inspect.ismethod(res):
Copy link
Contributor

@jreback jreback Jun 15, 2021

Choose a reason for hiding this comment

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

if callable(res) ?

res = getattr(self, func)
if inspect.isfunction(res) or inspect.ismethod(res):
return res()
return res
Copy link
Contributor

@jreback jreback Jun 15, 2021

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?

Copy link
Member Author

@jbrockmendel jbrockmendel Jun 16, 2021

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

Copy link
Member

@rhshadrach rhshadrach Jul 15, 2021

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?

Copy link
Member Author

@jbrockmendel jbrockmendel Jul 16, 2021

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

Copy link
Member

@rhshadrach rhshadrach Jul 16, 2021

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

@jreback jreback added this to the 1.4 milestone Jun 25, 2021
@rhshadrach
Copy link
Member

@rhshadrach rhshadrach commented Jul 16, 2021

lgtm, can you merge master one more time.

@rhshadrach rhshadrach merged commit 1cbf344 into pandas-dev:master Jul 17, 2021
23 of 25 checks passed
@rhshadrach
Copy link
Member

@rhshadrach rhshadrach commented Jul 17, 2021

Thanks @jbrockmendel

sthagen added a commit to sthagen/pandas-dev-pandas that referenced this issue Jul 17, 2021
@jbrockmendel jbrockmendel deleted the bug-gb-apply branch Jul 17, 2021
Joeperdefloep pushed a commit to Joeperdefloep/pandas that referenced this issue Sep 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.