Reduction operation 'argmax' not allowed for this dtype

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
3 min

banner

# Reduction operation 'argmax' not allowed for this dtype

The Pandas "TypeError: reduction operation 'argmax' not allowed for this dtype" occurs when the cell values in your DataFrame are not numeric before calling argmin(), argmax(), idxmin() or idxmax().

To solve the error, use the pandas.to_numeric() method to convert the non-numeric columns to numeric before calling idxmax() or argmax().

Here is an example of how the error occurs.

main.py
import pandas as pd df = pd.DataFrame({ 'experience': ['5', '14', '7', '10'], 'salary': [175.1, 180.2, 190.3, 205.4], }, index=['Alice', 'Bobby', 'Carl', 'Dan']) # ⛔️ TypeError: reduction operation 'argmax' not allowed for this dtype print(df.idxmax())

type error reduction operation argmax not allowed for this dtype

Notice that the experience column contains string values.

The cell values have to be numeric for you to be able to call the idxmin(), idxmax(), argmin() and argmax().

You can use the pandas.to_numeric() method to convert the values in the column to numeric before calling idxmax().

main.py
import pandas as pd df = pd.DataFrame({ 'experience': ['5', '14', '7', '10'], 'salary': [175.1, 180.2, 190.3, 205.4], }, index=['Alice', 'Bobby', 'Carl', 'Dan']) df['experience'] = pd.to_numeric(df['experience']) print(df.idxmax())
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
experience Bobby salary Dan dtype: object

convert cell values to numeric before calling idxmax

We used the pandas.to_numeric() method to convert the values in the "experience" column to numeric.

main.py
df['experience'] = pd.to_numeric(df['experience'])

Note that you have to repeat this process for all non-numeric columns.

You can view the data types of your columns by using the dtypes attribute.

main.py
import pandas as pd df = pd.DataFrame({ 'experience': ['5', '14', '7', '10'], 'salary': [175.1, 180.2, 190.3, 205.4], }, index=['Alice', 'Bobby', 'Carl', 'Dan']) # experience object # salary float64 # dtype: object print(df.dtypes)
The code for this article is available on GitHub

Once all cell values in your DataFrame have a numeric type, you will be able to call the argmin(), argmax(), idxmin() and idxmax() methods.

Notice that the type of the "experience" column in the example is object.

The aforementioned methods cannot be used with cell values of type object.

# Converting all object columns in the DataFrame to numeric

If you need to convert all object columns in the DataFrame to numeric, use the dtypes.eq() method.

main.py
import pandas as pd df = pd.DataFrame({ 'experience': ['5', '14', '7', '10'], 'another': ['1', '3', '14', '5'], 'salary': [175.1, 180.2, 190.3, 205.4], }, index=['Alice', 'Bobby', 'Carl', 'Dan']) object_columns = df.columns[df.dtypes.eq(object)] print(object_columns) df[object_columns] = df[object_columns].apply( pd.to_numeric, errors='coerce', axis=0 ) print('-' * 50) print(df.idxmax())
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
Index(['experience', 'another'], dtype='object') -------------------------------------------------- experience Bobby another Carl salary Dan dtype: object

convert all object columns in dataframe to numeric

We used the DataFrame.dtypes.eq() method to select all object columns in the DataFrame.

main.py
object_columns = df.columns[df.dtypes.eq(object)] # Index(['experience', 'another'], dtype='object') print(object_columns)

You can then convert all object columns to numeric by using the DataFrame.apply() method.

main.py
df[object_columns] = df[object_columns].apply( pd.to_numeric, errors='coerce', axis=0 )

The DataFrame.apply() method applies a function along an axis in the DataFrame.

We applied the pandas.to_numeric method to each row in the example.

# Using the DataFrame.astype() method to solve the error

You can also use the DataFrame.astype() method to solve the error.

main.py
import pandas as pd df = pd.DataFrame({ 'experience': ['5', '14', '7', '10'], 'salary': [175.1, 180.2, 190.3, 205.4], }, index=['Alice', 'Bobby', 'Carl', 'Dan']) df['experience'] = df['experience'].astype(int) # experience Bobby # salary Dan # dtype: object print(df.idxmax())

using dataframe astype method to solve the error

The code for this article is available on GitHub

The DataFrame.astype() method casts a Pandas object to the specified dtype.

We converted the cells in the experience to integers in the example, but you could also use the float type.

main.py
import pandas as pd df = pd.DataFrame({ 'experience': ['5', '14', '7', '10'], 'salary': [175.1, 180.2, 190.3, 205.4], }, index=['Alice', 'Bobby', 'Carl', 'Dan']) df['experience'] = df['experience'].astype(float) # experience Bobby # salary Dan # dtype: object print(df.idxmax())

using astype method with conversion to float

The code for this article is available on GitHub

However, using the pandas.to_numeric() method from the first subheading is usually the recommended approach.

The to_numeric() method tries to change non-numeric objects (e.g. strings) into integers or floating-point numbers.

# 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.