Fixing the issue – Numpy isnan() fails on an array of floats (from pandas dataframe apply)

Numpy isnan() fails on an array of floats (from pandas dataframe apply) is an error which occurs when you use np.isnan with numpy object arrays.

I will explain why this error takes place and how to fix it, while also trying to add other solutions that could solve the error.

Exploring the Error : Numpy isnan() fails on an array of floats (from pandas dataframe apply)

This is an error which occurs when you use np.isnan with numpy object arrays which are not accepted.

Please double check so you can avoid mixing between different errors. The error message should look like the error message bellow.

                                                                       #
TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
                                                                       #

Bellow we will describe how the error can be solved. With multiple possible solutions.

Solution : use pd.isnull instead of np.isnan

The solution to this problem is to use pd.isnull instead of np.isnan. pd.isnull takes both native dtypes and NumPy object arrays. But np.isnan only takes native dtype, like np.float32 or np.float64.

So, instead of using np.isnan like in this line of code.

                                                                       #
np.isnan(np.array([np.nan, 0], dtype=object))
                                                                       #

You can use pd.isnull just like this, the result is perfect execution with no errors.

                                                                       #
pd.isnull(np.array([np.nan, 0], dtype=object))
                                                                       #

Summing-up

The solutions above should be enough to solve your problem, I hope the article helped you get rid of the issue, please keep learning, keep coding and cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/