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: groupby(...).agg() with numpy transformations do not raise "Must produce aggregated value" #44845

Open
2 of 3 tasks
CRiddler opened this issue Dec 10, 2021 · 1 comment
Open
2 of 3 tasks
Labels
Apply Apply, Aggregate, Transform Bug Groupby

Comments

@CRiddler
Copy link

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the master branch of pandas.

Reproducible Example

import pandas as pd
import numpy as np

s = pd.Series(
    data=[1, 2, 3, 4, 5, 6],
    index=list("aabbcc"),
)

grouped = s.groupby(level=0)

# wrong result, should raise ValueError: Must produce aggregated value
# resultant shape conceptually incompatible with `.agg` 
grouped.agg("cumsum")
# a     1
# a     3
# b     3
# b     7
# c     5
# c    11
# dtype: int64

# wrong result, should raise ValueError: Must produce aggregated value
# resultant shape conceptually incompatible with `.agg` 
grouped.agg(np.cumsum)
# a     1
# a     3
# b     3
# b     7
# c     5
# c    11
# dtype: int64

# Correct result, raises ValueError: Must produce aggregated value
grouped.agg(lambda group: np.cumsum(group))
# ValueError: Must produce aggregated value

# Correct result, returns object Series with list values
grouped.agg(lambda group: np.cumsum(group).tolist())
# a     [1, 3]
# b     [3, 7]
# c    [5, 11]
# dtype: object

# Correct result, raises ValueError: Must produce aggregated value
grouped.agg(pd.Series.cumsum)
# ValueError: Must produce aggregated value

Issue Description

It appears that some non-reducing numpy function fastpaths bypass output checking and subsequently fail to raise a ValueError: Must produce aggregated value. In the cases that this ValueError is not raised, the resultant shape is the same as the input shape which is conceptually incompatible with the agg method (which should always reduce the input shape).

Since the result of the lambda wrapped numpy function raises the expected ValueError, I am assuming that this has to do with some fastpath coded to intercept direct numpy functions.

Expected Behavior

In these examples, I expect each one (except for the one that returns a list object) to raise the aforementioned error. All of these examples produce the same pattern of results if we swap out "cumsum" for "cumprod".

Installed Versions

INSTALLED VERSIONS

commit : 945c9ed
python : 3.9.7.final.0
python-bits : 64
OS : Linux
OS-release : 4.4.0-19041-Microsoft
Version : #1237-Microsoft Sat Sep 11 14:32:00 PST 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.3.4
numpy : 1.21.4
pytz : 2021.3
dateutil : 2.8.2
pip : 21.3.1
setuptools : 59.4.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : 7.30.1
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.5.0
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.7.3
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@CRiddler CRiddler added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 10, 2021
@rhshadrach
Copy link
Member

Thanks for the report - this would be resolved by #35725, but that is blocked from being acted on currently. Perhaps this issue can be resolved in the meantime, and so I think it should remain open.

@rhshadrach rhshadrach added Apply Apply, Aggregate, Transform Groupby and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 16, 2021
@rhshadrach rhshadrach added this to the Contributions Welcome milestone Dec 16, 2021
@github-actions github-actions bot added the Stale label Jan 16, 2022
@mroeschke mroeschke removed the Stale label Jan 17, 2022
@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform Bug Groupby
Projects
None yet
Development

No branches or pull requests

3 participants