pythonpython-3.xnumpyindex-error

only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices


I am implementing fft and when I shuffle the data elements using bit reversal, I get the following error:

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis 
(`None`) and integer or boolean arrays are valid indices.

My code is:

def shuffle_bit_reversed_order(data: np.ndarray) -> np.ndarray:
    x = data.size
    n = x / 2
    y = n * np.mod(x, 2)
    data[x], data[y] = data[y], data[x]
    return data

I think the problem is my data is of type 'float64' and I may have used it as an integer but I don't know how I can solve it.


Solution

  • I believe your problem is this: in your while loop, n is divided by 2, but never cast as an integer again, so it becomes a float at some point. It is then added onto y, which is then a float too, and that gives you the warning.