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.

"SpecificationError: nested dictionary is ambiguous in aggregation" in a certain case of groupby-aggregation

See original GitHub issue

All of these examples for using agg work fine:

import pandas as pd
df = pd.DataFrame({"A":['A','A','B','B','B'],
                   "B":[1,2,1,1,2],
                   "C":[9,8,7,6,5]})
df.groupby('A')[['B','C']].agg({'B':'sum','C':'count'})
df.groupby('A')[['B','C']].agg({'B':['sum','count'],'C':'count'})
df.groupby('A')[['B']].agg('sum')
df.groupby('A')['B'].agg('sum')

This one throws a future warning as mentioned here:

df.groupby('A')['B'].agg({'B':['sum','count']})

This one works just fine:

df.groupby('A')[['B','C']].agg({'B':'sum'})

But this one throws an error (I’m aware this expression isn’t necessary):

df.groupby('A')[['B']].agg({'B':'sum'})

SpecificationError: nested dictionary is ambiguous in aggregation

Why does it throw this error?

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None python: 3.6.8.final.0 python-bits: 64 OS: Windows OS-release: 10 machine: AMD64 processor: Intel64 Family 6 Model 79 Stepping 1, GenuineIntel byteorder: little LC_ALL: None LANG: en LOCALE: None.None

pandas: 0.24.1 pytest: 3.9.1 pip: 19.0.1 setuptools: 40.8.0 Cython: 0.29.5 numpy: 1.15.4 scipy: 1.2.0 pyarrow: None xarray: None IPython: 7.2.0 sphinx: 1.8.4 patsy: 0.5.1 dateutil: 2.7.5 pytz: 2018.9 blosc: None bottleneck: 1.2.1 tables: 3.4.4 numexpr: 2.6.9 feather: None matplotlib: 3.0.2 openpyxl: 2.6.0 xlrd: 1.2.0 xlwt: 1.3.0 xlsxwriter: 1.1.2 lxml.etree: 4.3.1 bs4: 4.7.1 html5lib: 1.0.1 sqlalchemy: 1.2.18 pymysql: None psycopg2: None jinja2: 2.10 s3fs: None fastparquet: 0.2.1 pandas_gbq: None pandas_datareader: None gcsfs: None

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
stuartebergcommented, Sep 9, 2019

I can confirm in pandas 0.25.0.

These work fine:

In [27]: df = pd.DataFrame({'x': [1,1,2], 'y': [3,4,4]})

In [28]: df
Out[28]:
   x  y
0  1  3
1  1  4
2  2  4

In [29]: df.groupby('x').agg({'x': 'sum'})
Out[29]:
   x
x
1  2
2  2

In [30]: df.groupby('y').agg({'x': 'sum'})
Out[30]:
   x
y
3  1
4  3

But this raises an error:

In [31]: df.groupby('y')[['x']].agg({'x': 'sum'})

...

SpecificationError: nested dictionary is ambiguous in aggregation

And the .agg() call only requires a single column, but you provide more than one column when you select columns from the groupby, then you get a weird result. (Why does the result below include a y column at all?)

In [34]: df.groupby('y')[['x', 'y']].agg({'x': 'sum'})
Out[34]:
   x
   x  y
y
3  1  3
4  3  8
0reactions
talukder-sowrovcommented, Dec 6, 2022

Take

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python groupby nested dictionary is ambiguous in aggregation
I.e., when vars is a list with SEVERAL elements, the aggregation works fine, o.w. it breaks with the same error "nested dictionary is...
Read more >
[Code]-Python groupby nested dictionary is ambiguous in ...
I am currently working on my thesis and facing some problems in a groupby function I ... SpecificationError: nested dictionary is ambiguous in...
Read more >
Deprecation of using a dict in groupby().agg({...}) to rename ...
Hi all,. As a heads up, we are deprecating the use of a dictionary in the agg method on groupby/resample/rolling objects.
Read more >
What's new in 0.25.0 (July 18, 2019) - Pandas
Pandas has added special groupby behavior, known as “named aggregation”, for naming the output columns when applying multiple aggregation functions to specific ......
Read more >
Python Nested Dictionary - GeeksforGeeks
Nested Dictionary : Nesting Dictionary means putting a dictionary inside another dictionary. Nesting is of great use as the kind of ...
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