TypeError when indexing numpy array using numba
<p>I need to sum up elements in a 1D <code>numpy</code> array (below: <code>data</code>) based on another array with information on class memberships (<code>labels</code>). I use <code>numba</code>in the code below to speed it up. However, If I dot not explicitly cast with <code>int()</code> in the line <code>ret[int(find(labels, g))] += y</code>, I reveice an error message:</p>

<p><code>TypeError: unsupported array index type ?int64</code></p>

<p>Is there a better workaround that explicit casting?</p>

<pre><code>import numpy as np
from numba import jit

labels = np.array([45, 85, 99, 89, 45, 86, 348, 764])
n = int(1e3)
data = np.random.random(n)
groups = np.random.choice(a=labels, size=n, replace=True)

@jit(nopython=True)
def find(seq, value):
    for ct, x in enumerate(seq):
        if x == value:
            return ct

@jit(nopython=True)
def subsumNumba(data, groups, labels):
    ret = np.zeros(len(labels))
    for y, g in zip(data, groups):
        # not working without casting with int()
        ret[int(find(labels, g))] += y
    return ret
</code></pre>

<p>The problem is that <code>find</code> can either return an <code>int</code> or <code>None</code> if it doesn't find anything, thus I think the <code>?int64</code> error. To avoid casting, you need to provide an <code>int</code> return value when <code>find</code> exits without finding the desired value and then handle it in the caller. </p>

python numba