pandas: False positive for "nested renamer is not supported" error

Code Sample

import pandas as pd

df = pd.DataFrame({'A': [1, 1, 1, 2, 2],  'B': range(5), 'C': range(5)})
df.groupby('A').agg({'B': 'sum', 'G': 'min'})  # aggregate by a non existing column

produces

<ipython-input-5-f5ac34bf856f> in <module>
----> 1 df.groupby('A').agg({'B': 'sum', 'G': 'min'})

~/src/dgr00/.venv3/lib/python3.6/site-packages/pandas/core/groupby/generic.py in aggregate(self, func, *args, **kwargs)
    938         func = _maybe_mangle_lambdas(func)
    939
--> 940         result, how = self._aggregate(func, *args, **kwargs)
    941         if how is None:
    942             return result

~/src/dgr00/.venv3/lib/python3.6/site-packages/pandas/core/base.py in _aggregate(self, arg, *args, **kwargs)
    364                     obj.columns.intersection(keys)
    365                 ) != len(keys):
--> 366                     raise SpecificationError("nested renamer is not supported")
    367
    368             from pandas.core.reshape.concat import concat

SpecificationError: nested renamer is not supported

Problem description

While groupby.agg() with a dictionary when renaming was deprecated in 1.0 ( https://pandas.pydata.org/pandas-docs/stable/whatsnew/v0.20.0.html#deprecate-groupby-agg-with-a-dictionary-when-renaming) the corresponding error message can also be obtained when aggregating by an non existing column which can lead to confusion.

Expected Output

Error saying that the column G does not exist.

Output of pd.show_versions()

INSTALLED VERSIONS

python : 3.6.4.final.0 OS : Linux machine : x86_64 pandas : 1.0.1

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 15
  • Comments: 16 (4 by maintainers)

Most upvoted comments

Instead of using .agg({'B': 'sum', 'G': 'min'}), try passing it as a list of tuples like .agg([('B', 'sum'), ('G', 'min')]).

Same problem here, bug is due to non-existing column

I also ran into this error and as mentioned, it was caused by trying to aggregate a non-existent column. Version 1.0.3

You wrote G instead of C. ā€˜G’ is nothing. ā€˜C’ is the column. import pandas as pd df = pd.DataFrame({'A': [1, 1, 1, 2, 2], 'B': range(5), 'C': range(5)}) df.groupby('A').agg({'B': 'sum', 'C': 'min'})

image

Applying multiple aggregation a column df = pd.DataFrame({'A': [1, 1, 1, 2, 2], 'B': range(5), 'C': range(5)}) df.groupby('A').agg({'B': ['sum','min'], 'C': ['sum','min']})

image

We are expecting the appropriate error message. The current error message is not pointing to the right direction.

thanks a lot, upstairs