TypeError: ufunc 'isnan' not supported for the input types

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
4 min

banner

# Table of Contents

  1. TypeError: ufunc 'isnan' not supported for the input types
  2. Using the pandas.isnull() method to solve the error
  3. Converting the strings to floats before calling numpy.isnan

# TypeError: ufunc 'isnan' not supported for the input types

The Python "TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types" occurs when you use the numpy.isnan() method with an unsupported dtype such as object or string.

To solve the error, use the pandas.isnull() method instead or convert the values in the array to floats.

Here is an example of how the error occurs.

main.py
import numpy as np arr = np.array(['5', '10', '15']) # ⛔️ 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'' result = np.isnan(arr) print(result)

type error ufunc isnan not supported for the input types

The code for this article is available on GitHub

The numpy.isnan() method tests the given array for NaN values and returns the result as a boolean array.

However, the method doesn't support values with a dtype (data type) of object or string.

Notice that the array in the example contains string values.

# Using the pandas.isnull() method to solve the error

One way to solve the error is to use the pandas.isnull method.

First, make sure you have the pandas module installed.

Open your terminal and run the following command.

shell
pip install pandas pip3 install pandas

Now import the module and use the pandas.isnull method.

main.py
import numpy as np import pandas as pd arr1 = np.array(['5', '10', '15']) # [False False False] print(pd.isnull(arr1)) arr2 = np.array(['5', '10', '15', None, np.nan]) # [False False False True True] print(pd.isnull(arr2))

using pandas isnull to solve the error

The code for this article is available on GitHub

The pandas.isnull method works as expected with string values because it supports category dtypes.

The method takes an array and indicates whether values are missing (NaN in numeric arrays, None or NaN in object arrays, NaT in datetime-like).

The method returns an array-like object of booleans indicating whether each element is missing.

You can also use the pandas.isna() method.

main.py
import numpy as np import pandas as pd arr1 = np.array(['5', '10', '15']) # [False False False] print(pd.isna(arr1)) arr2 = np.array(['5', '10', '15', None, np.nan]) # [False False False True True] print(pd.isna(arr2))

using pandas isna method to solve error

The code for this article is available on GitHub

The pandas.isna() method also detects the missing values in an array.

The method returns a boolean array of the same size that indicates if the values are NA.

NA values (None or numpy.NaN) get mapped to True values in the array.

All other values get mapped to False.

Note that empty strings "" or numpy.inf are not considered NA values unless you set pandas.options.mode.use_inf_as_na to True.

main.py
import numpy as np import pandas as pd arr = np.array(['5', None, np.nan, np.inf]) # [False True True False] print(pd.isna(arr)) pd.options.mode.use_inf_as_na = True # [False True True True] print(pd.isna(arr))

use inf as na

The code for this article is available on GitHub

# Converting the strings to floats before calling numpy.isnan

You can also convert the strings to floating-point numbers before calling numpy.isnan().

main.py
import numpy as np arr = np.array(['5', '10', '15', None, np.nan]) arr = arr.astype(float) print(arr) # 👉️ [ 5. 10. 15. nan nan] print(arr.dtype) # float64 # [False False False True True] print(np.isnan(arr))

We used the numpy.astype method to convert the values in the array to floating-point numbers before calling numpy.isnan().

We could've also used the dtype argument to convert the array elements to floating-point numbers.

main.py
import numpy as np arr = np.array( ['5', '10', '15', None, np.nan], dtype=float ) # arr = arr.astype(float) print(arr) # 👉️ [ 5. 10. 15. nan nan] print(arr.dtype) # float64 # [False False False True True] print(np.isnan(arr))
The code for this article is available on GitHub

Either way, we are no longer passing string values to the numpy.isnan() method, so everything works as expected.

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