[Numpy-discussion] Any and all NaNs

Keith Goodman kwgoodman at gmail.com
Fri May 23 13:22:09 EDT 2008


On Fri, May 23, 2008 at 10:16 AM, Keith Goodman <kwgoodman at gmail.com> wrote:
> I'm writing unit tests for a module that contains matrices. I was
> surprised that these are True:
>
>>> import numpy.matlib as mp
>>> x = mp.matrix([[mp.nan]])
>>> x.any()
>   True
>>> x.all()
>   True
>
> My use case is (x == y).all() where x and y are the same matrix except
> that x contains one NaN. Certianly x and y are not equal.
>
>>> x = mp.asmatrix(range(4)).reshape(2,2)
>>> y = mp.asmatrix(range(4)).reshape(2,2)
>>> x[0,0] = mp.nan
>>> (x == y).all()
>   True

Sorry. Ignore the last example. I used integers. Here's the example with floats:

>> x = 1.0 * mp.asmatrix(range(4)).reshape(2,2)
>> y = 1.0 * mp.asmatrix(range(4)).reshape(2,2)
>> x[0,0] = mp.nan
>> x

matrix([[ NaN,   1.],
        [  2.,   3.]])
>> (x == y).all()
   False

But the first example

>> x = mp.matrix([[mp.nan]])
>> x
   matrix([[ NaN]])
>> x.all()
   True
>> x.any()
   True

is still surprising.



More information about the NumPy-Discussion mailing list