question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NamedAgg error: aggregate() missing 1 required positional argument: 'func'

See original GitHub issue

Code 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
MarcoGorellicommented, May 18, 2020

Pretty sure this is the same as the issue I opened here, so I’ll copy over the example I put there:

import pandas as pd

df = pd.DataFrame(
    {"group": ["a"], "col": [1.0]}, index=[pd.to_datetime("2019-11-04 10:32:09.737")]
)
df.groupby("group").resample("1D").agg(open=pd.NamedAgg("col", "first"))

all will close that one

0reactions
GhostLeecommented, Jul 25, 2020

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 code

resampled_bookings = bookings.resample('BMS').agg(
    {
        "identity_id": pd.Series.nunique,
        "booking_id": pd.Series.count,
        "distance_m": pd.Series.sum
    }
)

or use multiple group func

resampled_bookings = bookings.resample('BMS').agg(
    {
        "identity_id": [pd.Series.nunique],
        "booking_id": [pd.Series.count],
        "distance_m": [pd.Series.sum]
    }
)

or give clear indication of func param

resampled_bookings = bookings.resample('BMS').agg(
   func= {
        "identity_id": [pd.Series.nunique],
        "booking_id": [pd.Series.count],
        "distance_m": [pd.Series.sum]
    }
)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Pandas GroupBy.agg() throws TypeError: aggregate() missing ...
Pandas GroupBy.agg() throws TypeError: aggregate() missing 1 required positional argument: 'arg' · Ask Question. Asked 3 years ...
Read more >
aggregate() missing 1 required positional argument: - YouTube
Pandas : Pandas GroupBy.agg() throws TypeError: aggregate () missing 1 required positional argument : 'arg' [ Beautify Your Computer ...
Read more >
class 'TypeError'>: aggregate() missing 1 required positional ...
Coding example for the question : aggregate() missing 1 required positional argument: 'func_or_funcs'-pandas.
Read more >
pandas.DataFrame.agg — pandas 1.5.2 documentation
Aggregate using one or more operations over the specified axis. Parameters. funcfunction, str, list or dict. Function to use for aggregating the data....
Read more >
A complete guide on Pandas Grouping, Aggregating, and ...
In pandas, the groupby function can be combined with one or more aggregation functions to quickly and easily summarize data.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found