Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy.isnan(value) not the same as value == numpy.nan?

Tags:

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?

like image 558
Alexander McFarlane Avatar asked Apr 09 '15 01:04

Alexander McFarlane


People also ask

Why does NP NaN == NP NaN return false?

The statements give False because math. nan , np. nan and float('nan') all have different ids. They do not have the same identity.

How do you compare NaN numpy?

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.

How do I know if my numpy value is NaN?

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.

How do I replace NaN with NP NaN?

In NumPy, to replace missing values NaN ( np. nan ) in ndarray with other numbers, use np. nan_to_num() or np. isnan() .


1 Answers

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.

like image 173
user2357112 supports Monica Avatar answered Oct 13 '22 19:10

user2357112 supports Monica