Skip to content

Groupby with multiple aggregations doesn't pass through positional or keyword arguments. #26611

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

Open
TomAugspurger opened this issue Jun 1, 2019 · 1 comment · May be fixed by #57706
Open

Comments

@TomAugspurger
Copy link
Contributor

TomAugspurger commented Jun 1, 2019

We pass positional *args through to the aggfunc for a single grouper.

In [1]: import pandas as pd

In [2]: df = pd.DataFrame({"A": [1, 2]})

In [3]: def f(x, y):
   ...:     return x.sum() + y
   ...:

In [4]: df.groupby([0, 0]).agg(f, 10)
Out[4]:
    A
0  13

But not for multiple.

In [5]: df.groupby([0, 0]).agg([f], 10)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-c8273728b28e> in <module>
----> 1 df.groupby([0, 0]).agg([f], 10)

~/sandbox/pandas/pandas/core/groupby/generic.py in aggregate(self, arg, *args, **kwargs)
   1354     @Appender(_shared_docs['aggregate'])
   1355     def aggregate(self, arg=None, *args, **kwargs):
-> 1356         return super().aggregate(arg, *args, **kwargs)
   1357
   1358     agg = aggregate

~/sandbox/pandas/pandas/core/groupby/generic.py in aggregate(self, func, *args, **kwargs)
    167         func = func2
    168
--> 169         result, how = self._aggregate(func, _level=_level, *args, **kwargs)
    170         if how is None:
    171             return result

~/sandbox/pandas/pandas/core/base.py in _aggregate(self, arg, *args, **kwargs)
    534             return self._aggregate_multiple_funcs(arg,
    535                                                   _level=_level,
--> 536                                                   _axis=_axis), None
    537         else:
    538             result = None

~/sandbox/pandas/pandas/core/base.py in _aggregate_multiple_funcs(self, arg, _level, _axis)
    592         # if we are empty
    593         if not len(results):
--> 594             raise ValueError("no results")
    595
    596         try:

ValueError: no results

Same for keyword arguments.

Is that intentional? It's a bit strange, since all your aggfuncs would need to be expecting the same positional and keyword arguments. I think we should recommend using something like functools.partial to get a unary function, and documents that args and kwargs aren't passed through for multi-agg.

@WillAyd
Copy link
Member

WillAyd commented Jun 2, 2019

I don't see this as being intentional or useful so for sure would be on board with any clean up here and pushing the partial approach if there is a need here

@rhshadrach rhshadrach self-assigned this Feb 13, 2021
@rhshadrach rhshadrach removed their assignment Feb 28, 2021
@rhshadrach rhshadrach linked a pull request Mar 2, 2024 that will close this issue
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants