How to Fix: TypeError: cannot perform reduce with flexible type
In this article we will discuss TypeError: cannot perform reduce with flexible type and how can we fix it. This error may occur when we find the mean for a two-dimensional NumPy array which consists of data of multiple types.
Dataset in use: Student ID Student Name Branch Marks 101 Harry CSE 87 102 Ron ECE 88 103 Alexa CSE 72
When we create this table using NumPy then this 2D Array consists of data with multiple types. In this, we had String and Integer datatypes. To find the mean value for any of the numeric columns like Marks, it throws TypeError, because it doesn’t know how to take mean when not all the values are numbers (i.e. Student Name, Branch consists data of type string).
Example: Error producing code
To overcome this problem create a 2D array using Pandas DataFrame instead of NumPy. Since DataFrame has an index value for each row and name for each column, it helps the interpreter to distinguish between columns of different types.
This single alternative fixes the issue efficiently.
Example: Fixed code
In the above example, dataframe mean value is generated for all columns with numeric type if the column name is not specified- student_ID and Marks columns are of type float. So it calculates the mean for those 2 columns and the rest of the columns are of type string. So it won’t calculate the mean value.