Description
-
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