NamedAgg error: aggregate() missing 1 required positional argument: 'func'
See original GitHub issueCode Sample, a copy-pastable example if possible
resampled_bookings = bookings.resample('BMS').agg(
unique_identity_ids=pd.NamedAgg(column="identity_id", aggfunc=pd.Series.nunique),
booking_count=pd.NamedAgg(column="booking_id", aggfunc=pd.Series.count),
booking_distance=pd.NamedAgg(column="distance_m", aggfunc=pd.Series.sum)
)
Problem description
This error prevents me from aggregating data and (re)naming columns at the same time. Instead, it is necessary to use the following approach, which isn’t altogether bad:
resampled_bookings = bookings.resample('BMS').agg({
"booking_id": "count",
"distance_m": sum,
"identity_id": pd.Series.nunique
})
resampled_bookings.rename(columns={
"booking_id": "booking_count",
"distance_m": "booking_distance_m",
"identity_id": "unique_identity_ids"
}, inplace=True)
Expected Output
The aggregate function would produce a new DataFrame with the following columns:
- booking_count
- booking_distance_m
- unique_identity_ids
Output of pd.show_versions()
INSTALLED VERSIONS
commit : None python : 3.7.5.final.0 python-bits : 64 OS : Linux OS-release : 5.3.0-19-generic machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8
pandas : 1.0.1 numpy : 1.18.1 pytz : 2019.3 dateutil : 2.8.1 pip : 18.1 setuptools : 40.8.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 : 2.11.1 IPython : 7.12.0 pandas_datareader: None bs4 : None bottleneck : None fastparquet : None gcsfs : None lxml.etree : None matplotlib : 3.2.0 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 : None xlwt : None xlsxwriter : None numba : None
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top GitHub Comments
Pretty sure this is the same as the issue I opened here, so I’ll copy over the example I put there:
all will close that one
you can see the comment in pandas.core.base.SelectionMixin._aggregate, line 320&331
pandas.core.base.SpecificationError: nested renamer is not supported
but u can use these codeor use multiple group func
or give clear indication of func param