You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the particular case of a groupby over 1 column ('c'), asking a subset of columns ('a' and 'b') and passing agg a function of 2 arguments ('mypring2'), a DataFrame of the full groups (including all columns) is passed to myprint2:
For every group, I would have expected 2 calls of myprint2, one with Series 'a' and one with Series 'b'. This is particularly confusing, because agg would have the expected behaviour if groupby is called over a list or if a function of 1 argument is passed to agg:
Expected Output
I would have expected the same as if groupby is called over a list or if a function of 1 argument is passed to agg:
>>>>df.groupby(['c', 'd'])[['a', 'b']].agg(myprint2, 0)
011223Name: a, dtype: int64344556Name: a, dtype: int64011120Name: b, dtype: int64314150Name: b, dtype: int64>>>>df.groupby(['c'])[['a', 'b']].agg(print)
011223Name: a, dtype: int64344556Name: a, dtype: int64011120Name: b, dtype: int64314150Name: b, dtype: int64
Output of pd.show_versions()
INSTALLED VERSIONS
commit : None
python : 3.7.4.final.0
python-bits : 64
OS : Windows
OS-release : 10
machine : AMD64
processor : Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.None
This is quite subtle, as when you supply a function to agg, the extra argument for your function makes it so that a DataFrame is passed. Here's an example, using the above DataFrame
In [108]: defonearg(x):
...: print('x is ', x)
...: print('x dtype is', type(x))
...: ifisinstance(x, pd.DataFrame):
...: print('numcols is ', len(x.columns))
...: else:
...: print("not a DataFrame")
...: returnx.sum()
...:
In [111]: deftwoargs(x, y):
...: print('x is ', x)
...: print('x dtype is', type(x))
...: ifisinstance(x, pd.DataFrame):
...: print('numcols is ', len(x.columns))
...: else:
...: print("not a DataFrame")
...: returnx.sum()
...:
The functions do the same thing, printing the argument, the type of the argument, and printing the number of columns if it is a DataFrame and then return the sum.
But the output is different:
In [109]: df.groupby('c')[['a', 'b']].agg(onearg)
xis011223Name: a, dtype: int64xdtypeis<class'pandas.core.series.Series'>notaDataFramexis344556Name: a, dtype: int64xdtypeis<class'pandas.core.series.Series'>notaDataFramexis011120Name: b, dtype: int64xdtypeis<class'pandas.core.series.Series'>notaDataFramexis314150Name: b, dtype: int64xdtypeis<class'pandas.core.series.Series'>notaDataFrameIn [112]: df.groupby('c')[['a', 'b']].agg(twoargs, 0)
xisabcd011xs121xs230xsxdtypeis<class'pandas.core.frame.DataFrame'>numcolsis4xisabcd341zd451zd560zdxdtypeis<class'pandas.core.frame.DataFrame'>numcolsis4
Without the extra argument, a series was passed in to the function. With the extra argument, the entire DataFrame was passed in.
The behavior here is not documented. So it might be intentional, or possibly a bug.
Dr-Irv
changed the title
agg behaviour depends on the number of arguments of the function or the number of columns in the groupby.
agg behaviour depends on the number of arguments of the function
Oct 11, 2020
Code Sample, a copy-pastable example if possible
Problem description
In the particular case of a groupby over 1 column ('c'), asking a subset of columns ('a' and 'b') and passing agg a function of 2 arguments ('mypring2'), a DataFrame of the full groups (including all columns) is passed to myprint2:
For every group, I would have expected 2 calls of myprint2, one with Series 'a' and one with Series 'b'. This is particularly confusing, because agg would have the expected behaviour if groupby is called over a list or if a function of 1 argument is passed to agg:
Expected Output
I would have expected the same as if groupby is called over a list or if a function of 1 argument is passed to agg:
Output of
pd.show_versions()
INSTALLED VERSIONS
commit : None
python : 3.7.4.final.0
python-bits : 64
OS : Windows
OS-release : 10
machine : AMD64
processor : Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.None
pandas : 1.0.3
numpy : 1.18.2
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 46.1.1
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
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : None
xlsxwriter : None
numba : None
The text was updated successfully, but these errors were encountered: