Skip to content

Instantly share code, notes, and snippets.

@chris-b1
Created July 18, 2018 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris-b1/0049c54ce8cf37257002ab41b278f7d9 to your computer and use it in GitHub Desktop.
Save chris-b1/0049c54ce8cf37257002ab41b278f7d9 to your computer and use it in GitHub Desktop.
%%cython
cimport numpy as cnp
import numpy as np
cimport cython
cdef extern from "numpy/npy_math.h":
bint npy_isnan(double x) nogil
cdef inline bint isnan(double x) nogil:
return x != x
@cython.boundscheck(False)
@cython.wraparound(False)
def isnan_crt(double[:] arr):
cdef:
Py_ssize_t i, N=arr.shape[0]
cnp.uint8_t[:] out = np.empty(N, dtype='uint8')
with nogil:
for i in range(N):
out[i] = npy_isnan(arr[i])
return np.asarray(out, dtype='bool')
@cython.boundscheck(False)
@cython.wraparound(False)
def isnan_inline(double[:] arr):
cdef:
Py_ssize_t i, N=arr.shape[0]
cnp.uint8_t[:] out = np.empty(N, dtype='uint8')
with nogil:
for i in range(N):
out[i] = isnan(arr[i])
return np.asarray(out, dtype='bool')
a = np.random.randn(int(1e7))
a[::1000] = np.nan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment