Last updated: Apr 12, 2024
Reading time·3 min
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.
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())
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()
.
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())
Running the code sample produces the following output.
experience Bobby salary Dan dtype: object
We used the pandas.to_numeric()
method to convert the values in the
"experience"
column to numeric.
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.
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)
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
.
object
columns in the DataFrame to numericIf you need to convert all object
columns in the DataFrame
to numeric, use
the dtypes.eq()
method.
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())
Running the code sample produces the following output.
Index(['experience', 'another'], dtype='object') -------------------------------------------------- experience Bobby another Carl salary Dan dtype: object
We used the DataFrame.dtypes.eq()
method to select all object columns in the
DataFrame
.
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.
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.
DataFrame.astype()
method to solve the errorYou can also use the DataFrame.astype() method to solve the error.
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())
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.
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())
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.
You can learn more about the related topics by checking out the following tutorials: