Check if array contains any nan (fast ways)
import numpy as np
import bottleneck as bn
@numba.jit(nopython=True)
def anynan(a):
for ai in a.flat:
if np.isnan(ai):
return True
return False
a = np.arange(1000000,dtype=float)
a[-1] = np.nan
%timeit np.any(np.isnan(a))
%timeit np.isnan(np.argmin(a)) # early stop
%timeit np.isnan(np.sum(a))
%timeit np.isnan(np.einsum('i->',a))
%timeit np.isnan(np.min(a))
%timeit bn.anynan(a) # early stop
%timeit anynan(a) # early stop
548 µs ± 8.97 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
943 µs ± 32.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
904 µs ± 79.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
580 µs ± 8.83 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
379 µs ± 6.11 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
967 µs ± 31.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
943 µs ± 60.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
a = np.arange(1000000,dtype=float)
a[0] = np.nan
%timeit np.any(np.isnan(a))
%timeit np.isnan(np.argmin(a))
%timeit np.isnan(np.sum(a))
%timeit np.isnan(np.einsum('i->',a))
%timeit np.isnan(np.min(a))
%timeit bn.anynan(a)
%timeit anynan(a)
640 µs ± 40.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
4.4 µs ± 220 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
1.13 ms ± 181 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
600 µs ± 13.9 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
421 µs ± 22 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
395 ns ± 21.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
437 ns ± 14.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)