Since Numba 0.19 one is able to explicitly create numpy arrays in nopython mode. How can I create an array of a given type?
from numba import jit
import numpy as np
@jit(nopython=True)
def f():
a = np.zeros(5, dtype = np.int)
The above code fails with the following error
TypingError: Failed at nopython (nopython frontend)
Undeclared Function(<built-in function zeros>)(int32, Function(<class 'int'>))
File "<ipython-input-4-3169be7a8201>", line 6
You should use numba
dtypes instead of numpy
import numba
import numpy as np
@numba.njit
def f():
a = np.zeros(5, dtype=numba.int32)
return a
In [8]: f()
Out[8]: array([0, 0, 0, 0, 0], dtype=int32)
In python 2.7 it seems that np.int does work.
As for the workaround from dlenz, I'd like to point that using np.int32 does work as well... with the added advantage that the code will work without modifications if you want to remove numba.njit at some point for whatever reason.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With