Numba with numpy string arrays

Hi, below is a minimal working example for my code:

import numba
import numpy as np
from numba import njit

@njit
def str_test(acceptor,donor,is_orig_def=False,is_new_def=True):
    _N=acceptor.shape[0]
    label=np.empty(_N,dtype=np.dtype('S10'))
    for i in range(_N):
        if is_orig_def:
           label[i]="D"*np.sum(donor)+"A"*np.sum(acceptor)
        elif is_new_def:
           label[i]="D"*np.sum(donor)+"X"*np.sum(acceptor)
    
    return label

acceptor=np.array([[1,1],[2,0]])
donor=np.array([[0,1],[1,1]])

print(str_test(acceptor,donor))

The error is

No implementation of function Function(<built-in function setitem>) found for signature:
 
 >>> setitem(array([char x 10], 1d, C), int64, unicode_type)
 
There are 16 candidate implementations:
   - Of which 16 did not match due to:
   Overload of function 'setitem': File: <numerous>: Line N/A.
     With argument(s): '(array([char x 10], 1d, C), int64, unicode_type)':
    No match.
---------
def str_test(acceptor,donor,is_orig_def=False,is_new_def=True):
    <source elided>
        if is_orig_def:
           label[i]="D"*np.sum(donor)+"A"*np.sum(acceptor)
           ^

I think the error is largely due how I have defined the numpy string array, but anyone have any recommendations on how to correctly set up a numpy string array in @njit mode?

The error mentions you’re trying to assign a unicode string into a byte array element. Try this

label = np.empty(_N, dtype=np.dtype('U10'))

Thanks! That idea just simply escaped me!