Skip to content

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

Open
@TomAugspurger

Description

@TomAugspurger

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions