Why am I getting the following:
>>> v nan >>> type(v) <type 'numpy.float64'> >>> v == np.nan False >>> np.isnan(v) True
I would have thought the two should be equivalent?
The statements give False because math. nan , np. nan and float('nan') all have different ids. They do not have the same identity.
To check for NaN values in a Numpy array you can use the np. isnan() method. This outputs a boolean mask of the size that of the original array. The output array has true for the indices which are NaNs in the original array and false for the rest.
To test array for NaN, use the numpy. isnan() method in Python Numpy. Returns True where x is NaN, false otherwise. This is a scalar if x is a scalar.
In NumPy, to replace missing values NaN ( np. nan ) in ndarray with other numbers, use np. nan_to_num() or np. isnan() .
nan != nan
. That's just how equality comparisons on nan
are defined. It was decided that this result is more convenient for numerical algorithms than the alternative. This is specifically why isnan
exists.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With