Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. import numpy as np
  2. cimport numpy as np
  3. cimport cython
  4.  
  5. @cython.boundscheck(False) # Deactivate bounds checking
  6. @cython.wraparound(False) # Deactivate negative indexing.
  7. cpdef np.ndarray[np.double_t, ndim=1] \
  8. shift(
  9. np.ndarray[np.double_t, ndim=1] G, # Sorted groupby array
  10. np.ndarray[np.double_t, ndim=1] D, # Data array
  11. int N # Periods to shift
  12. ):
  13.  
  14. # Definitions
  15. cdef int i
  16. cdef int i_prev
  17. cdef int l = D.shape[0]
  18. cdef np.ndarray[np.double_t, ndim=1] R = np.empty(l, dtype=np.double)
  19.  
  20. for i in range(l):
  21. i_prev = i - N
  22. if (i_prev < 0) or (G[i] != G[i_prev]):
  23. R[i] = np.nan
  24. else:
  25. R[i] = D[i_prev]
  26.  
  27. return R
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement