Python\Numpy: Comparing arrays with NAN

numpy, python

Solution

Since `a` and `b` are lists, `a == b` isn't returning an array, and so your numpy-like logic won't work:

>>> a == b
False

The command you've quoted only works if they're arrays:

>>> a,b = np.asarray(a), np.asarray(b)
>>> a == b
array([ True, False], dtype=bool)
>>> (a == b) | (np.isnan(a) & np.isnan(b))
array([ True,  True], dtype=bool)
>>> ((a == b) | (np.isnan(a) & np.isnan(b))).all()
True

which should work to compare two arrays (either they're both equal or they're both NaN).

Problem

Why are the following two lists not equal? ``` a = [1.0, np.NAN] b = np.append(np.array(1.0), [np.NAN]).tolist() ``` I am using the following to check for identicalness. ``` ((a == b) | (np.isnan(a) & np.isnan(b))).all(), np.in1d(a,b) ``` Using `np.in1d(a, b)` it seems the `np.NAN` values are not equal but I am not sure why this is. Can anyone shed some light on this issue?

Original source

Related problems