Issue
Consider the simplest possible function
@numba.jit
def foo(s1):
return s1
Now constructing an array of np.bytes_
objects
> a = np.array(['abc']*5, dtype='S5')
> a
array([b'abc', b'abc', b'abc', b'abc', b'abc'], dtype='|S5')
Why does calling foo
with the vector work:
> foo(a)
array([b'abc', b'abc', b'abc', b'abc', b'abc'], dtype='|S5')
But calling foo
with a single element raises an exception
> foo(a[0])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_8124/2559272744.py in <module>
----> 1 foo(a[0])
TypeError: bad argument type for built-in operation
(This is running numba 0.54.1 from conda-forge on Windows with Python 3.9.7 and numpy 1.20.3)
Solution
Neither bytes
nor np.bytes_
types are listed in the set of types supported by numba
as of the latest release. The closest things it supports would be:
- Character sequences (read:
str
) (though it specifically says "no operations are available on them", so this is pretty useless); your function would work if you calledfoo(a[0].decode())
to make it text (but only because it's a pretty useless function) - Actual
numpy
arrays; the cost to view thebytes
/np.bytes_
as annp.array
is pretty low, so you could just do:foo(np.frombuffer(a[0], np.uint8))
and produce something that is more programmatically useful and represents the same data.
Answered By - ShadowRanger
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.