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

Borislav Hadzhiev

Last updated: Apr 11, 2024

Reading time·4 min

- TypeError: ufunc 'isnan' not supported for the input types
- Using the
`pandas.isnull()`

method to solve the error - Converting the strings to floats before calling
`numpy.isnan`

**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)`

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.

`pandas.isnull()`

method to solve the errorOne 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))`

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))`

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))`

The code for this article is available on GitHub

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

You can learn more about the related topics by checking out the following tutorials:

- TypeError: Image data cannot be converted to float [Solved]
- OverflowError: Python int too large to convert to C long
- How to use numpy.argsort in Descending order in Python
- TypeError: cannot pickle '_thread.lock' object [Solved]
- ValueError: DataFrame constructor not properly called [Fix]
- ufunc 'add' did not contain loop with signature matching types
- TypeError: Field elements must be 2- or 3-tuples, got 1
- ValueError: Expected 2D array, got 1D array instead [Fixed]
- ValueError: Found array with dim 3. Estimator expected 2
- ValueError: columns overlap but no suffix specified [Solved]
- Columns have mixed types. Specify dtype option on import
- Convert a Row to a Column Header in a Pandas DataFrame
- Drop Unnamed: 0 columns from a Pandas DataFrame in Python
- Arrays used as indices must be of integer (or boolean) type
- Shape mismatch: objects cannot be broadcast to a single shape
- Usecols do not match columns, columns expected but not found
- Pandas: Select distinct across multiple DataFrame columns
- Convert column Values to Columns in a Pandas DataFrame