Fail to use np.diff in njit mode

I tried to use np.diff() in njit mode, which is said to be supported. However, I encountered an error.


Both arr and arr1 are np array of float64. I simply copy past the displayed value of arr to get arr1. It works for arr1 but not arr. Could you please tell me how I can solve this error?
Thank you very much.

Error message one

Error message two

The error tells you that reshape is only supported on contiguous arrays. According to the error message reshape is used internally within diff. The issue is that arr is a non-contiguous array which can happen when you slice arrays. It is likely happening when you do stock0_per_trade_list[3] (but stock0_per_trade_list could be already non-contiguous, you have to check). When you copy the printed output to create arr1 this is obviously a newly allocated array which is always contiguous. There are many options to solve this. The simplest but not necessarily most efficient one is to do stock0_per_trade_list = np.ascontiguousarray(stock0_per_trade_list) before you use it.

1 Like

Thank you very much for your help. Yes np.ascontiguousarray() solved the problem. I will compare performance using np.ascontiguousarray() and performance using a self-defined diff function. The only concern for the self-defined diff function is its speed when dealing with large array.

There is no reason why your own diff implementation should be slower on any kind of array. In fact, it will most likely be faster because you can customize it to your specific needs. A basic np.diff in Numba for 1-dimensional arrays (also non-contiguous ones) is just:

@nb.njit 
def diff(a):
    return a[1:] - a[:-1]
1 Like

That’s true. Many thanks for your insight.