Last updated: Apr 8, 2024
Reading timeยท4 min
:
), ellipsis (...
), numpy.newaxis (None
) and integer or boolean arrays are valid indicesThe 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.
Here is an example of how the error occurs.
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])
We used a float to index a NumPy array which caused the error.
int()
class to convert the float to an integerYou can use the int()
class to convert a floating-point number to an integer.
import numpy as np arr = np.array([1, 2, 3]) my_float = 1.0 print(arr[int(my_float)]) # ๐๏ธ 2
The int() class returns an integer object constructed from the provided number or string argument.
The constructor returns 0
if no arguments are given.
//
to get an integer when dividingYou might commonly get a float value if you use the division operator.
# ๐๏ธ division print(10 / 5) # ๐๏ธ 2.0 # ๐๏ธ floor division print(10 // 5) # ๐๏ธ 2
Division /
of integers yields a float, while
floor division //
of integers results
in an integer.
# ๐๏ธ floor division always returns an integer result = 10 // 5 print(result) # ๐๏ธ 2 print(type(result)) # ๐๏ธ <class 'int'>
floor()
function applied to the result.You can also use slices or integer arrays to index a NumPy array.
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]
The first example selects the first 2 elements in the NumPy array.
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
.
The error is also raised when you index a NumPy array with a list of floating-point numbers.
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.
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])
We used a list comprehension to iterate over the list of floating-point numbers.
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.
If you aren't sure what type a variable stores, use the built-in type()
class.
my_float = 1.0 print(type(my_float)) # ๐๏ธ <class 'float'>
The type() class returns the type of an object.
You can learn more about the related topics by checking out the following tutorials:
:
), ellipsis (...
), numpy.newaxis (None
) and integer or boolean arrays are valid indices