The error **IndexError: arrays used as indices must be of integer (or boolean) type **shows up when you use an array of float or string type as an index to an array.

Let’s try to understand this error in a bit more detail.

Table of Contents

## What causes the IndexError: arrays used as indices must be of integer (or boolean) type?

Numpy arrays must have indices of type int or boolean. When indices of some other type, like floats or strings, are used, you see an error.

Consider the following example, let’s say we have an array [[0,1,1.1],[0,2,2.1]]. We have to use the first column values as indices to extract the value from another array.

import numpy as np arr_indices=np.array([[0,1,1.1],[0,2,2.1]]) arr=np.array([[1,2,3],[4,5,6]]) #extract the first column values indices=arr_indices[:,0] # access the arr with extracted indices print(arr[indices])

Output:

Traceback (most recent call last): File "C:\Users\paian\PycharmProjects\PythonHowTo\venv\Solutions\IndexError.py", line 6, in <module> print(arr[indices]) IndexError: arrays used as indices must be of integer (or boolean) type

Explanation:

In the above program, we extract the values in the first column from the array arr_indices. Now, we use these values to index another array, arr. We see that Python raises **IndexError: arrays used as indices must be of integer (or boolean) type.**

Although the values extracted from the array arr_indices look like integers, they’re not. They are of the type ‘float’.

In numpy, if the array contains even one element of type float, then all the elements will be converted to type float.

Have a look at the below code:

import numpy as np arr_indices=np.array([[0,1,1.1],[0,2,2.1]]) indices=arr_indices[:,0] print("Type of indices : ",indices.dtype)

Output:

Type of indices : float64

Now that we’ve understood the problem, let’s try to fix it.

## Fix 1: Convert the array to int type

Convert the array you’re planning to use as an index to type int. To do so, use astype()

array.astype(int)

Now, let’s convert the array to an integer type and execute the program.

import numpy as np arr_indices=np.array([[0,1,1.1],[0,2,2.1]]) arr=np.array([[1,2,3],[4,5,6]]) indices=arr_indices[:,0].astype(int) print(arr[indices])

Output:

[[1 2 3] [1 2 3]]

## Fix 2: Convert the array to Bool type(if it makes sense)

You can also convert the array to be used as an index to a boolean type. **Do this only if it makes sense.**

To convert the array to Bool type, use astype() as shown below.

array.astype(bool)

In the below code snippet, we convert the array to be used as the index to boolean format. Only the values corresponding to the index value True are considered.

import numpy as np arr_indices=np.array([[0,1,1.1],[1,2,2.1]]) arr=np.array([[1,2,3],[4,5,6]]) indices=arr_indices[:,0].astype(bool) print(indices) print(arr[indices])

Output:

[False True] [[4 5 6]]

## Conclusion

In this article, we’ve discussed two fixes to overcome the **IndexError: arrays used as indices must be of integer (or boolean) type. **You can either convert the array to type int or boolean to fix the error.

We hope this article was informative. Kindly comment and let us know the fix that helped you overcome this IndexError.

Do you know how to create a checkerboard pattern using numpy arrays? Click on the link to learn more.

Thanks for your blog, nice to read. Do not stop.