[FIX] TypeError: only integer scalar arrays can be converted to a scalar index

You see the TypeError: only integer scalar arrays can be converted to a scalar index in Python when you’re working with numpy for one of the following reasons.

  • If you’re trying to index a list with a numpy array instead of a numpy array element. 
  • When a numpy method is expecting dimension, and you provide the array instead.
  • While using numpy.concatenate(), you pass arrays instead of tuples or lists.

Now, let’s try to understand the causes with detailed examples.

Also check, How to fix IndexError: arrays used as indices must be of integer (or boolean) type.

 

What causes the TypeError: only integer scalar arrays can be converted to a scalar index?

Case 1: Indexing a list with a numpy array

An integer or boolean numpy array can be used to index another numpy array. Refer to the Official numpy guide for more details. Check the below code snippet to understand better.

import numpy as np

indices=np.array([1,2,3,4,5])
arr=np.array([6,7,8,9,10,11])

print(arr[indices])

Output:

[ 7  8  9 10 11]

 

However, in Python lists, a proper integer index value must be provided. Array, list objects are not accepted as valid index parameter. Refer to the below code snippet for more details.

import numpy as np

indices=np.array([1,2,3,4,5])
lst= [6,7,8,9,10,11]

print(lst[indices])

Output:

Traceback (most recent call last):
  File "C:\Users\paian\PycharmProjects\PythonHowTo\venv\Solutions\numpy arrays.py", line 6, in <module>
    print(lst[indices])
TypeError: only integer scalar arrays can be converted to a scalar index

Case 2: Providing an array where the dimension is expected as a parameter.

Let’s say you’re trying to create a numpy array using some method. The numpy method expects a dimension as an input parameter. If you pass a numpy array by mistake, the above error is seen.

Consider the below example.

np.indices(np.random.rand(3,3))

Output:

Traceback (most recent call last):
  File "C:\Users\paian\PycharmProjects\PythonHowTo\venv\Solutions\numpy arrays.py", line 11, in <module>
    np.indices(np.random.rand(3,3))
  File "C:\Users\paian\PycharmProjects\PythonHowTo\venv\lib\site-packages\numpy\core\numeric.py", line 1771, in indices
    res = empty((N,)+dimensions, dtype=dtype)
TypeError: only integer scalar arrays can be converted to a scalar index

Case 3: Passing arrays in numpy.concatenate()

The numpy.concatenate() expects the lists or tuples. But if you provide arrays, the above error shows up. Consider the below code snippet.

a=np.array([1,2,3,4,5])
b=np.array([6,7,8,9])

print(np.concatenate(a,b))

Output:

Traceback (most recent call last):
  File "C:\Users\paian\PycharmProjects\PythonHowTo\venv\Solutions\numpy arrays.py", line 17, in <module>
    print(np.concatenate(a,b))
  File "<__array_function__ internals>", line 180, in concatenate
TypeError: only integer scalar arrays can be converted to a scalar index

Now, that ve understood what causes this error, let’s figure out different ways of fixing this error.

Possible Solutions to TypeError: only integer scalar arrays can be converted to a scalar index.

Case 1: Indexing a list with a numpy array element or using a numpy array instead of a list.

If you’re seeing the issue because you’re indexing a list with a numpy array, then consider indexing the array element instead.

import numpy as np

indices=np.array([1,2,3,4,5])
lst= [6,7,8,9,10,11]

print(lst[indices[0]])

Output:

7

If this is not an option, use a numpy array instead of a list.

import numpy as np

indices=np.array([1,2,3,4,5])
arr=np.array([6,7,8,9,10,11])

print(arr[indices])

Output:

[ 7  8  9 10 11]

 

Case 2: Provide dimension instead of an array

If the expected input parameter is a dimension, you can pass np_array.shape to return the dimension. Refer to the below code snippet:

print(np.indices(np.random.rand(3,3).shape))

Output:

[[[0 0 0]
  [1 1 1]
  [2 2 2]]

 [[0 1 2]
  [0 1 2]
  [0 1 2]]]

Case 3: While using numpy.concatenate() pass the elements of arrays as list or tuples.

When you’re using numpy.concatenate() method, pass the elements of the array as a list or tuple.

In the below code snippet, we are passing the array elements as lists.

import numpy as np

a=np.array([1,2,3,4,5])
b=np.array([6,7,8,9])

print(np.concatenate([a,b]))

Output:

[1 2 3 4 5 6 7 8 9]

 

In the below example, we are passing the array elements as tuples.

import numpy as np

a=np.array([1,2,3,4,5])
b=np.array([6,7,8,9])

print(np.concatenate((a,b)))

Output:

[1 2 3 4 5 6 7 8 9]

That’s all. We hope you enjoyed reading.

Kindly comment and let us know what worked in your case.

Happy Pythoning!

If you enjoyed reading, share this article.

Anusha Pai is a Software Engineer having a long experience in the IT industry and having a passion to write. She has a keen interest in writing Python Errorfixes, Solutions, and Tutorials.

1 thought on “[FIX] TypeError: only integer scalar arrays can be converted to a scalar index”

Leave a Comment