Last updated: Apr 11, 2024
Reading time·4 min

pandas.isnull() method to solve the errornumpy.isnanThe 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.
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 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.
pip install pandas pip3 install pandas
Now import the module and use the pandas.isnull method.
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 pandas.isnull method works as expected with string values because it
supports category dtypes.
The method returns an array-like object of booleans indicating whether each element is missing.
You can also use the pandas.isna() method.
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 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.
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))

numpy.isnanYou can also convert the strings to floating-point numbers before calling
numpy.isnan().
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.
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))
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: