Assuming this is the numpy array, with shape (3,3):
ar = np.array([1, 2, 3, 4, np.nan, 5, np.nan, 6, np.inf]).reshape((3,3))
print (ar)
[[ 1. 2. 3.]
[ 4. nan 5.]
[nan 6. inf]]
To check for NaN, positive infinity, negative infinity, or different combinations of those, we can use:
numpy.isnan(ar) # True wherever nan
numpy.isposinf(ar) # True wherever pos-inf
numpy.isneginf(ar) # True wherever neg-inf
numpy.isinf(ar) # True wherever pos-inf or neg-inf
~numpy.isfinite(ar) # True wherever pos-inf or neg-inf or nan
respectively. Each of these returns a bool array, and passing the bool array to numpy.where() gives us two index arrays (one index array per dimension of ar):
ar_nan = np.where(np.isnan(ar))
print (ar_nan)
(array([1, 2], dtype=int64), array([1, 0], dtype=int64)) # Means, nans at (1,1) and (2,0)
and
ar_inf = np.where(np.isinf(ar))
print (ar_inf)
(array([2], dtype=int64), array([2], dtype=int64)) # Means, inf is at (2,2)
Also, to see the limits of float64:
np.finfo(np.float64)
Answer from fountainhead on Stack Overflowfinfo(resolution=1e-15, min=-1.7976931348623157e+308, max=1.7976931348623157e+308, dtype=float64)
Ray's solution is good. However, on my machine it is about 2.5x faster to use numpy.sum in place of numpy.min:
In [13]: %timeit np.isnan(np.min(x))
1000 loops, best of 3: 244 us per loop
In [14]: %timeit np.isnan(np.sum(x))
10000 loops, best of 3: 97.3 us per loop
Unlike min, sum doesn't require branching, which on modern hardware tends to be pretty expensive. This is probably the reason why sum is faster.
edit The above test was performed with a single NaN right in the middle of the array.
It is interesting to note that min is slower in the presence of NaNs than in their absence. It also seems to get slower as NaNs get closer to the start of the array. On the other hand, sum's throughput seems constant regardless of whether there are NaNs and where they're located:
In [40]: x = np.random.rand(100000)
In [41]: %timeit np.isnan(np.min(x))
10000 loops, best of 3: 153 us per loop
In [42]: %timeit np.isnan(np.sum(x))
10000 loops, best of 3: 95.9 us per loop
In [43]: x[50000] = np.nan
In [44]: %timeit np.isnan(np.min(x))
1000 loops, best of 3: 239 us per loop
In [45]: %timeit np.isnan(np.sum(x))
10000 loops, best of 3: 95.8 us per loop
In [46]: x[0] = np.nan
In [47]: %timeit np.isnan(np.min(x))
1000 loops, best of 3: 326 us per loop
In [48]: %timeit np.isnan(np.sum(x))
10000 loops, best of 3: 95.9 us per loop
I think np.isnan(np.min(X)) should do what you want.