Skip to content

Groupby doesn't call aggregation on empty groups #18869

Closed
@TomAugspurger

Description

@TomAugspurger

This should exit the interpreter.

In [1]: import pandas as pd

In [2]: df = pd.DataFrame({"A": pd.Categorical(['a', 'a'], categories=['a', 'b']),
   ...:                    "B": [1, 1]})
   ...:

In [3]: import sys

In [4]: def f(x):
   ...:     if len(x) == 0:
   ...:         sys.exit(1)
   ...:     return len(x)
   ...:

In [5]: df.groupby('A').agg(f)
Out[5]:
     B
A
a  2.0
b  NaN

Instead, I think groupby assumes the output of the custom aggfunc is NaN.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions