HOWTO · Python
How to Fix IndexError: Invalid Index to Scalar Variable
Diagnose and fix NumPy's IndexError: invalid index to scalar variable by checking whether an indexed value is a scalar or an array.
On this page
IndexError: invalid index to scalar variable means that an indexing operation has already produced one scalar value, and the next [...] tries to index that scalar again. In NumPy, inspect the array’s ndim, shape, and the value you selected before adding another index.
Inspect the Array and the Selected Value
A one-dimensional NumPy array needs one position to select an item. values[0] is a NumPy scalar, not a one-element array, so values[0][1] fails even though values itself has several elements. Checking the selected value’s type makes the problem visible.
import numpy as np
values = np.array([1, 2, 3, 4, 5])
selected = values[0]
print(values.ndim, values.shape)
print(type(selected).__name__)
print(values[3])
Output:
1 (5,)
int64
4
The exact scalar class can vary by NumPy build and data type; the important point is that selected has no array dimension left to index. Use the index on values only as far as its dimensions allow.
Reproduce the Error and Fix a One-Dimensional Array
The following boundary case catches the diagnostic so the rest of the example can run. The first subscript selects the integer 1; the second subscript is therefore invalid. Remove the extra subscript when you want the item, or slice the original array when you need another array.
import numpy as np
values = np.array([1, 2, 3, 4, 5])
try:
print(values[0][1])
except IndexError as error:
print(error)
print(values[0])
print(values[0:2])
Output:
invalid index to scalar variable.
1
[1 2]
values[0] returns one scalar. By contrast, values[0:2] uses a slice and returns a one-dimensional array, which you can index again if that is genuinely required. Do not fix this error by adding arbitrary brackets: decide whether the program needs one value or a smaller array.
Index a Two-Dimensional NumPy Array
A two-dimensional array has a row and a column, so two indices are valid while they address the original array. Prefer the tuple form grid[row, column]; it makes the two-dimensional operation clear and avoids visually confusing a chain of scalar selections with a multidimensional index.
import numpy as np
grid = np.array([[1, 2, 3], [4, 5, 6]])
print(grid.ndim, grid.shape)
print(grid[1, 2])
print(grid[1][2])
Output:
2 (2, 3)
6
6
Both indexing forms return 6, but grid[1, 2] expresses the intended row-and-column lookup directly. A third index, such as grid[1, 2][0], tries to index the scalar 6 and raises this diagnostic.
Distinguish Scalar Indexing From an Out-of-Bounds Error
This message is different from an out-of-bounds error. values[9] asks for an element that is not present and reports that index 9 is out of bounds. values[0][1] first reaches a valid element, then incorrectly treats that scalar as a container.
When debugging, print array.ndim and array.shape, then split a long expression into intermediate variables. Check the type or shape after every selection. This approach also catches a common source of the bug: code that expects a two-dimensional result but receives one row, one column, or one aggregated scalar instead.
For the underlying indexing rules, consult the NumPy indexing documentation and the NumPy scalar type reference. These examples use NumPy; ordinary Python lists can raise different messages, so inspect the actual object before applying the fix.