close

[Solved] Getting TypeError: reduction operation ‘argmax’ not allowed for this dtype when trying to use idxmax()

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Getting TypeError: reduction operation ‘argmax’ not allowed for this dtype when trying to use idxmax() in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Getting TypeError: reduction operation ‘argmax’ not allowed for this dtype when trying to use idxmax() Error Occurs?

Today I get the following error Getting TypeError: reduction operation ‘argmax’ not allowed for this dtype when trying to use idxmax() in python.

How To Solve Getting TypeError: reduction operation ‘argmax’ not allowed for this dtype when trying to use idxmax() Error ?

  1. How To Solve Getting TypeError: reduction operation 'argmax' not allowed for this dtype when trying to use idxmax() Error ?

    To Solve Getting TypeError: reduction operation 'argmax' not allowed for this dtype when trying to use idxmax() Error The type of the cell values are, by default, non-numeric. argmin()idxmin()argmax() and other similar functions need the dtypes to be numeric.

  2. Getting TypeError: reduction operation 'argmax' not allowed for this dtype when trying to use idxmax()

    To Solve Getting TypeError: reduction operation 'argmax' not allowed for this dtype when trying to use idxmax() Error The type of the cell values are, by default, non-numeric. argmin()idxmin()argmax() and other similar functions need the dtypes to be numeric.

Solution 1

#best_c = results_table.loc[results_table['Mean recall score'].idxmax()]['C_parameter']

We should replace this line of code

The main problem:

1) the type of “mean recall score” is object, you can’t use “idxmax()” to calculate the value 2) you should change “mean recall score” from “object ” to “float” 3) you can use apply(pd.to_numeric, errors = ‘coerce’, axis = 0) to do such things.

best_c = results_table
best_c.dtypes.eq(object) # you can see the type of best_c
new = best_c.columns[best_c.dtypes.eq(object)] #get the object column of the best_c
best_c[new] = best_c[new].apply(pd.to_numeric, errors = 'coerce', axis=0) # change the type of object
best_c
best_c = results_table.loc[results_table['Mean recall score'].idxmax()]['C_parameter'] #calculate the mean values

Solution 2

The type of the cell values are, by default, non-numeric. argmin()idxmin()argmax() and other similar functions need the dtypes to be numeric.

The easiest solution is to use pd.to_numeric() in order to convert your series (or columns) to numeric types. An example with a data frame df with a column 'a' would be:

df['a'] = pd.to_numeric(df['a'])

A more complete answer on type casting on pandas can be found here.

Hope that helps 🙂

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read