Pandas SpecificationError: nested renamer is not supported

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
5 min

banner

# Pandas SpecificationError: nested renamer is not supported

The "pandas.errors.SpecificationError: nested renamer is not supported" error occurs when you use the older, outdated syntax when calling the DataFrame.agg() method.

To solve the error, call the agg() method with keyword arguments that map names and functions.

Here is an example of how the error occurs.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'salary': [175.1, 180.2, 190.3, 205.4], }) # ⛔️ pandas.errors.SpecificationError: nested renamer is not supported series = df['salary'].agg({'salary': ['min', 'max']})

pandas specification error nested renamer is not supported

Passing a dictionary with a nested renamer is not supported in recent Pandas versions.

However, you could pass keyword arguments to the DataFrame.agg method to achieve the same result.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'salary': [175.1, 180.2, 190.3, 205.4], }) series = df['salary'].agg(min='min', max='max') # min 175.1 # max 205.4 # Name: salary, dtype: float64 print(series) print(series[0]) # 👉️ 175.1 print(series[1]) # 👉️ 205.4

pass keyword arguments to agg to solve the error

The code for this article is available on GitHub

We passed 2 keyword arguments that represent mappings from names to functions to the agg() method.

This is the currently supported syntax and you can pass as many keyword arguments to the method as necessary.

If you need to convert the result to a DataFrame, use the pandas.DataFrame() constructor.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'salary': [175.1, 180.2, 190.3, 205.4], }) df2 = pd.DataFrame(df['salary'].agg(min='min', max='max')) # salary # min 175.1 # max 205.4 print(df2)

convert result to dataframe by using pandas dataframe constructor

The code for this article is available on GitHub

# The same syntax can be used when calling agg() on a GroupBy object

The same syntax can be used when you need to call the agg() method on a GroupBy object.

main.py
import pandas as pd df = pd.DataFrame({ 'name': [ 'Alice', 'Alice', 'Bobby', 'Bobby', 'Carl', 'Carl', ], 'salary': [175.1, 205.1, 180.2, 350.2, 190.3, 500.1], }) # min max # name # Alice 175.1 205.1 # Bobby 180.2 350.2 # Carl 190.3 500.1 print(df.groupby(['name'])['salary'].agg(min='min', max='max'))

calling agg on a groupby object correctly

The code for this article is available on GitHub

# Make sure the specified columns exist

When calling the agg() method, make sure the specified columns exist.

The first call to agg() in the example below returns a DataFrame successfully.

However, the second call raises an error because the experience column doesn't exist in the DataFrame.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'salary': [175.1, 180.2, 190.3, 205.4], }) # salary 205.4 # dtype: float64 print(df.agg({'salary': 'max'})) # ⛔️ Error. The `experience` column does not exist print(df.agg({'salary': 'max', 'experience': 'min'}))

Make sure to only pass existing column keys to the agg() method.

# You can also pass a list of functions to the agg() method

You can also pass a list of functions to the agg() method.

main.py
import pandas as pd df = pd.DataFrame({ 'name': [ 'Alice', 'Alice', 'Bobby', 'Bobby', 'Carl', 'Carl', ], 'salary': [175.1, 205.1, 180.2, 350.2, 190.3, 500.1], }) print(df.groupby(['name'])['salary'].agg(['max', 'min', 'mean']))
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
max min mean name Alice 205.1 175.1 190.1 Bobby 350.2 180.2 265.2 Carl 500.1 190.3 345.2

passing a list to the agg method

If you need to rename the columns after calling agg(), use the columns attribute.

main.py
import pandas as pd df = pd.DataFrame({ 'name': [ 'Alice', 'Alice', 'Bobby', 'Bobby', 'Carl', 'Carl', ], 'salary': [175.1, 205.1, 180.2, 350.2, 190.3, 500.1], }) df2 = df.groupby(['name'])['salary'].agg(['max', 'min', 'mean']) print(df2) print('-' * 50) df2.columns = ['max_salary', 'min_salary', 'mean_salary'] print(df2)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
max min mean name Alice 205.1 175.1 190.1 Bobby 350.2 180.2 265.2 Carl 500.1 190.3 345.2 -------------------------------------------------- max_salary min_salary mean_salary name Alice 205.1 175.1 190.1 Bobby 350.2 180.2 265.2 Carl 500.1 190.3 345.2

rename columns after calling agg

Passing a list of functions to the agg() method is allowed, however, passing a nested dictionary is not.

Therefore, we can just call the method with a list of functions and rename the columns if necessary.

# Make sure you haven't called agg() with duplicated function names without assigning a column name

The error also occurs when you call the agg() method with duplicated function names without assigning a column name.

main.py
import pandas as pd df = pd.DataFrame({ 'name': [ 'Alice', 'Alice', 'Bobby', 'Bobby', 'Carl', 'Carl', ], 'salary': [175.1, 205.1, 180.2, 350.2, 190.3, 500.1], }) # ⛔️ pandas.errors.SpecificationError: Function names must be unique if there is no new column names assigned df2 = df.groupby(['name']).agg(['max', 'min', 'min']) print(df2)

Notice that we passed the min function twice to agg().

Make sure you haven't forgotten to select a specific column.

For example, the following is valid.

main.py
import pandas as pd df = pd.DataFrame({ 'name': [ 'Alice', 'Alice', 'Bobby', 'Bobby', 'Carl', 'Carl', ], 'salary': [175.1, 205.1, 180.2, 350.2, 190.3, 500.1], }) df2 = df.groupby(['name'])['salary'].agg(['max', 'min', 'min']) # max min min # name # Alice 205.1 175.1 175.1 # Bobby 350.2 180.2 180.2 # Carl 500.1 190.3 190.3 print(df2)
The code for this article is available on GitHub

The following is also valid because we no longer have duplicate function names in the call to agg().

main.py
import pandas as pd df = pd.DataFrame({ 'name': [ 'Alice', 'Alice', 'Bobby', 'Bobby', 'Carl', 'Carl', ], 'salary': [175.1, 205.1, 180.2, 350.2, 190.3, 500.1], }) df2 = df.groupby(['name']).agg(['max', 'min']) # salary # max min # name # Alice 205.1 175.1 # Bobby 350.2 180.2 # Carl 500.1 190.3 print(df2)

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.