I’m trying to write a function decorated with @guvectorize
which accepts numpy recarrays. It does work successfully with @njit
, but I would like to use @guvectorize
since it plays nicely with Dask. I’ve tried the following:
import numba as nb
import numpy as np
size = 10
arr = np.rec.fromarrays([np.arange(size), np.random.uniform(size=size), np.random.uniform(size=size)], names=['x', 'y', 'z'])
ty = nb.from_dtype(arr.dtype)
@nb.njit
def test_njit(a, b, out):
for i in range(len(a)):
# ...dummy example
out[i]['x'] = a[i]['x']
out[i]['y'] = a[i]['y'] * b
out[i]['z'] = a[i]['z'] / b
@nb.guvectorize([(ty[:], nb.float64, ty[:])], '(n),()->(n)')
def test_guvec(a, b, out):
for i in range(len(a)):
# ...dummy example
out[i]['x'] = a[i]['x']
out[i]['y'] = a[i]['y'] * b
out[i]['z'] = a[i]['z'] / b
out1 = np.zeros_like(arr)
test_njit(arr, 2., out1) # This runs successfully
print(out1)
out2 = np.zeros_like(arr)
test_guvec(arr, 2., out2) # This fails
print(out2)
For test_guvec
, I get the following error: ufunc 'test_guvec' output (typecode 'V') could not be coerced to provided output parameter (typecode 'V') according to the casting rule ''same_kind''