Numpy comes with its own vectorized version or:
a[np.logical_or(np.isnan(a), np.isinf(a))] = -999
Despite the fact that the above version is understandable, it is faster, which is a bit strange:
a[np.isnan(aa)] = -9999
The idea behind this is that 'np.inf-np.inf = np.nan`
%timeit a[np.isnan(aa)] = -999
Therefore, the version | and np.logical_or seems internally equivalent
source share