Skip to content

groupby().agg() calls the user function for an extra time with empty inputs in 1.0.0 #31760

Closed
@icexelloss

Description

@icexelloss

Code Sample, a copy-pastable example if possible

df = pd.DataFrame({"key": ["a", "b", "c", "c"], "value": [1, 2, 3, 4]})                                                                                                                                                                                                                                                                                                                                                                                                                        

def foo(x): 
    print(f"called with {x}") 
    return len(x) 

df.groupby("key")["value"].agg(foo)

Problem description

In 1.0.0, this code outputs:

called with Series([], Name: value, dtype: int64)
called with 0    1
Name: value, dtype: int64
called with 1    2
Name: value, dtype: int64
called with 2    3
3    4
Name: value, dtype: int64
Out[10]: 
key
a    1
b    1
c    2
Name: value, dtype: int64

In 0.25.3, the code outputs:

In [5]:  df.groupby("key")["value"].agg(foo)                                                                                                                                                                                                                                                                                                                                                                                                                                                           
called with 0    1
Name: value, dtype: int64
called with 1    2
Name: value, dtype: int64
called with 2    3
3    4
Name: value, dtype: int64
Out[5]: 
key
a    1
b    1
c    2
Name: value, dtype: int64

In 1.0.0, foo is called one more time with empty input, which can break user code

Note: We receive a lot of issues on our GitHub tracker, so it is very possible that your issue has been posted before. Please check first before submitting so that we do not have to handle and close duplicates!

Note: Many problems can be resolved by simply upgrading pandas to the latest version. Before submitting, please check if that solution works for you. If possible, you may want to check if master addresses this issue, but that is not necessary.

For documentation-related issues, you can check the latest versions of the docs on master here:

https://pandas-docs.github.io/pandas-docs-travis/

If the issue has not been resolved there, go ahead and file it in the issue tracker.

Expected Output

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line]

Metadata

Metadata

Assignees

No one assigned

    Labels

    GroupbyRegressionFunctionality that used to work in a prior pandas version

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions