Python: can numba work with arrays of strings in nopython mode?
numba now supports str
(since version 0.41)
Strings are not yet supported by Numba (as of version 20.0). Actually, "character sequences are supported, but no operations are available on them".
Indeed, a possible workaround is to interpret characters as numbers. For ASCII characters this is straightforward, see the Python ord
and chr
functions. However, already for your minimal example, you end with functions that are a lot less readable:
import numpy as np
import numba
x=np.array(['some','text','this','is'])
@numba.jit(nopython=True)
def numba_str(txt):
x=0
for i in xrange(txt.shape[0]):
if (txt[i,0]==116 and # 't'
txt[i,1]==101 and # 'e'
txt[i,2]==120 and # 'x'
txt[i,3]==116): # 't'
x += 1
return x
print numba_str(x.view(np.uint8).reshape(-1, x.itemsize))