Search code examples
pythonnumpyindex-error

'IndexError: only integers, slices (`:`), ellipsis (`...`)' ...?


There are many questions with this error, but I have not been able to find where is the source of the problem in my code. The code I have is the following:

for i in segs:
    if relDiff(segs[i+1], segs[i]) > 0.05:
        arr_x[i] = 0; arr_y[i] = 0

I get the error on line three: IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices

segs is an array, and relDiff is a function I made that calculates the relative difference. Here is that function:

def relDiff(x,x_ref):
    return np.abs((x-x_ref)/x_ref)

Any help is very much appreciated!


Solution

  • Since you're using items from segs as indexes of arr_x and arr_y, segs has to be a list/array of integers. Otherwise, you'd have to convert each item to an integer, like arr_x[int(i)]. Whether or not that would logically make sense to do that in you program is up to you to determine, since your requirements are unknown. We don't know what segs contains.