-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: DataFrame.groupby.agg
has inconsistent behaviour depending on DataFrame.groupby
by
's Iterable length and use of DataFrame.groupby.agg
's *args
/**kwargs
#47092
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
Comments
DataFrame.groupby.agg
has inconsistent behaviour depending on DataFrame.groupby
by
Iterable length and use of DataFrame.groupby.agg
's *args
/**kwargs
DataFrame.groupby.agg
has inconsistent behaviour depending on DataFrame.groupby
by
's Iterable length and use of DataFrame.groupby.agg
's *args
/**kwargs
Thanks @sondalex for the report. This does look like a bug and the different codepath taken for 1 groupby key with args/kwargs specified is here. pandas/pandas/core/groupby/generic.py Lines 885 to 892 in 7c054d6
with cc @rhshadrach |
Thanks for the report @sondalex. This is similar to (but more thorough than) #39169. I agree that this is problematic and if there are to be different things passed to the UDF, the user should control this. The hard problem is how to go about changing behavior, partially because we don't know the UDFs users are using and partially because of how many ways this code can be hit (e.g. I believe |
Thanks @rhshadrach. Yes, I now see that you covered this in #39169 (comment) will close as duplicate
As a simpler reproducer based on the above (could still be simplified further) import pandas as pd
import numpy as np
data = {
"group_1": [1, 1, 2, 2, 3, 4],
"group_2": ["A", "B", "C", "C", "D", "D"],
"x": np.random.uniform(-10, 10, 6),
"y": np.random.uniform(0, 10, 6),
}
df = pd.DataFrame(data)
func = lambda x: type(x).__name__
func_with_kwarg = lambda x,y: type(x).__name__
grp= df.groupby(["group_1"])
print(grp.agg(func))
print(grp.agg(func_with_kwarg, y=1))
grp= df.groupby(["group_1", "group_2"])
print(grp.agg(func))
print(grp.agg(func_with_kwarg, y=1))
|
Pandas version checks
Reproducible Example
One element in
by
:The snippet above prints
Series
as intermediate objects.When
**kwargs
are used, intermediate objects areDataFrame
.More than one element in
by
:Here, no matter the use case, the intermediate objects are
Series
Issue Description
The intermediate objects' type is not consistent when
ITERABLE
provided indf.groupby(by=<ITERABLE>).agg(func = lambda x:print(x), y=1)
is of length one with respect to length bigger than one.Expected Behavior
When
ITERABLE
is of length one and**kwargs
/*args
, intermediate objects should also be expected to be instances ofSeries
.Installed Version
commit : 4bfe3d0
python : 3.10.0.final.0
python-bits : 64
OS : Linux
OS-release : 5.14.10-300.fc35.x86_64
Version : #1 SMP Thu Oct 7 20:48:44 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8
pandas : 1.4.2
numpy : 1.22.4
pytz : 2022.1
dateutil : 2.8.2
pip : 22.1.1
setuptools : 57.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 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
markupsafe : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
The text was updated successfully, but these errors were encountered: