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

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
4 min

banner

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

The Python "IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices" occurs when we use a non-supported type to index a NumPy array.

To solve the error, use the int() class to convert the value to an integer.

indexerror only integers slices ellipsis

Here is an example of how the error occurs.

main.py
import numpy as np arr = np.array([1, 2, 3]) my_float = 1.0 # โ›”๏ธ IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices print(arr[my_float])

cannot use float for indexing

We used a float to index a NumPy array which caused the error.

# Use the int() class to convert the float to an integer

You can use the int() class to convert a floating-point number to an integer.

main.py
import numpy as np arr = np.array([1, 2, 3]) my_float = 1.0 print(arr[int(my_float)]) # ๐Ÿ‘‰๏ธ 2

convert float to integer before accessing list

The int() class returns an integer object constructed from the provided number or string argument.

The constructor returns 0 if no arguments are given.

# Use floor division // to get an integer when dividing

You might commonly get a float value if you use the division operator.

main.py
# ๐Ÿ‘‡๏ธ division print(10 / 5) # ๐Ÿ‘‰๏ธ 2.0 # ๐Ÿ‘‡๏ธ floor division print(10 // 5) # ๐Ÿ‘‰๏ธ 2

using floor division

Division / of integers yields a float, while floor division // of integers results in an integer.

main.py
# ๐Ÿ‘‡๏ธ floor division always returns an integer result = 10 // 5 print(result) # ๐Ÿ‘‰๏ธ 2 print(type(result)) # ๐Ÿ‘‰๏ธ <class 'int'>
The result of using the floor division operator is that of a mathematical division with the floor() function applied to the result.

# Using slices or integer arrays to index a NumPy array

You can also use slices or integer arrays to index a NumPy array.

main.py
import numpy as np arr = np.array([[1, 2], [3, 4], [5, 6]]) # โœ… get the first 2 elements in the array print(arr[0: 2]) # ๐Ÿ‘‰๏ธ [[1 2] [3 4]] print('-' * 50) # ----------------------------------------- # โœ… get the first element of each subarray print(arr[:, 0]) # ๐Ÿ‘‰๏ธ [1 3 5] print('-' * 50) # ----------------------------------------- # โœ… get the first element of the first two nested arrays print(arr[0:2, 0]) # ๐Ÿ‘‰๏ธ [1 3]

using slices or integer arrays to index a numpy array

The first example selects the first 2 elements in the NumPy array.

Indexes are zero-based and the start index is inclusive, whereas the stop index is exclusive.

The second example selects the first element in each nested array.

The third example selects the first element in the first two nested arrays.

Python indexes are zero-based, so the first item in an array has an index of 0, and the last item has an index of -1 or len(array) - 1.

# Indexing a NumPy array with a list of floats

The error is also raised when you index a NumPy array with a list of floating-point numbers.

main.py
import numpy as np arr = np.array([[1, 2], [3, 4], [5, 6]]) a_list = [0.0, 2.0] # โ›”๏ธ IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices print(arr[a_list])

Notice that the values in the list are of type float, therefore they can't be used to index the NumPy array.

To solve the error, convert the values in the list to integers before using the list to index the array.

main.py
import numpy as np arr = np.array([[1, 2], [3, 4], [5, 6]]) a_list = [0.0, 2.0] a_list = [int(item) for item in a_list] # [[1 2] # [5 6]] print(arr[a_list])

convert values in list to integers

We used a list comprehension to iterate over the list of floating-point numbers.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we convert the float to an integer and return the result.

The last step is to use the list of integers to index the NumPy array.

# Checking what type a variable stores

If you aren't sure what type a variable stores, use the built-in type() class.

main.py
my_float = 1.0 print(type(my_float)) # ๐Ÿ‘‰๏ธ <class 'float'>

The type() class returns the type of an object.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev