Last updated: Apr 8, 2024
Reading timeΒ·3 min
The Python "IndexError: invalid index to scalar variable" occurs when we try to access a NumPy scalar like an integer or a float at a specific index.
To solve the error, make sure the value you are trying to index is an array or another sequence with the right dimensions.
Here is an example of how the error occurs.
import numpy as np arr = np.array([1, 2, 3]) print(arr[0]) # ποΈ 1 # βοΈ IndexError: invalid index to scalar variable. print(arr[0][0])
We accessed the array element at index 0
, which has a value of 1
.
Then, we tried to access 1
at index 0
which caused the error.
If you meant to access an item in an array, make sure the variable is an array and use a single set of square brackets.
import numpy as np arr = np.array([1, 2, 3]) print(arr[0]) # ποΈ 1 print(arr[1]) # ποΈ 2 print(arr[2]) # ποΈ 3
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(arr) - 1
.
If you meant to declare a two-dimensional array, use the following syntax.
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) # [[1 2 3] # [4 5 6]] print(arr) print(arr[0][0]) # ποΈ 1 print(arr[0][1]) # ποΈ 2 print(arr[0][2]) # ποΈ 3
We declared a two-dimensional array (an array of arrays). The array has 2 rows and each row has 3 columns.
The first set of square brackets gives us access to the first nested array
(index 0
).
The second set of square brackets allows us to access items in the nested array.
If you need to handle the error, use a try/except statement.
import numpy as np arr = np.array([1, 2, 3]) try: print(arr[0][0]) except IndexError: # ποΈthis runs print('The specified index does NOT exist')
We try to access a nested array at an index 0
and get an IndexError
because
the array element at index 0
is an integer.
The except
block runs where we can handle the error.
You can also ignore the error by using a pass
statement.
import numpy as np arr = np.array([1, 2, 3]) try: print(arr[0][0]) except IndexError: pass
The pass statement does nothing and is used when a statement is required syntactically but the program requires no action.
Print the variable you are trying to access at a specific index and make sure it contains what you expect.
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) print(arr) # ποΈ [[1 2 3] [4 5 6]] print(type(arr)) # ποΈ <class 'numpy.ndarray'>
The type of the variable should be an array, a tuple or a list for you to be able to access it at an index.
A common cause of the error is reassigning a variable that stores an array to a scalar (an integer or a float) before accessing the value at an index.
import numpy as np my_array = np.array([[1, 2, 3], [4, 5, 6]]) # ποΈ Reassigned the variable by mistake my_array = np.int32(100) # βοΈ IndexError: invalid index to scalar variable. print(my_array[0])
We reassigned the array to an integer before trying to access the value at a specific index which caused the error.
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