Only integer scalar arrays can be converted to a scalar index

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
4 min

banner

# Table of Contents

  1. Only integer scalar arrays can be converted to a scalar index
  2. Trying to slice a Python list with a NumPy array
  3. Use the numpy.array() method to convert the Python list to a NumPy array
  4. Calling the numpy.concatenate() method with multiple, individual arrays
  5. Passing an array instead of a shape to numpy.ndindex()

# Only integer scalar arrays can be converted to a scalar index

The NumPy "TypeError: only integer scalar arrays can be converted to a scalar index" occurs for 3 main reasons:

  1. Trying to slice a Python list with a NumPy array.
  2. Calling the numpy.concatenate() method with multiple, individual arrays. instead of a tuple containing multiple arrays.
  3. Passing an array instead of a shape to the numpy.ndindex() method.

# Trying to slice a Python list with a NumPy array

Here is an example of how the error occurs.

main.py
import numpy as np arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # ๐Ÿ‘‡๏ธ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print(arr) print(type(arr)) # ๐Ÿ‘‰๏ธ <class 'list'> indices = np.array([0, 2, 3]) # โ›”๏ธ TypeError: only integer scalar arrays can be converted to a scalar index print(arr[indices])

only integer scalar arrays can be converted to scalar index

The arr variable stores a native Python list.

You can use the type() class to verify this as shown in the code sample.

The type class returns the type of an object.

# Use the numpy.array() method to convert the Python list to a NumPy array

To solve the error in this case, we have to use the numpy.array() method to convert the Python list to an array.

main.py
import numpy as np arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # โœ… Convert the Python list to a NumPy array arr = np.array(arr) # ๐Ÿ‘‡๏ธ [0 1 2 3 4 5 6 7 8 9] print(arr) print(type(arr)) # ๐Ÿ‘‰๏ธ <class 'numpy.ndarray'> indices = np.array([0, 2, 3]) print(arr[indices]) # ๐Ÿ‘‰๏ธ [0 2 3]

use numpy array method to convert list to array

The code for this article is available on GitHub

The code sample uses the numpy.array() method to convert the Python list to an array.

This enables us to use a NumPy array of indices to index the other NumPy array.

Note that you cannot use NumPy array indexing to index a native Python list.

We could've also converted the list to an array right before using NumPy array indexing.

main.py
import numpy as np arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # ๐Ÿ‘‡๏ธ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print(arr) print(type(arr)) # ๐Ÿ‘‰๏ธ <class 'list'> indices = np.array([0, 2, 3]) print(np.array(arr)[indices]) # ๐Ÿ‘‰๏ธ [0 2 3]

convert list to array right before using numpy array indexing

The code for this article is available on GitHub

We used the numpy.array() method to convert the list to an array right before using a NumPy array of indices.

# Calling the numpy.concatenate() method with multiple, individual arrays

You will also get the error if you call numpy.concatenate with multiple, individual arrays.

main.py
import numpy as np arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) # โ›”๏ธ TypeError: only integer scalar arrays can be converted to a scalar index arr3 = np.concatenate(arr1, arr2) print(arr3)

calling numpy concatenate method with multiple individual arrays

The error in the example is called because we passed multiple, separate arrays to the numpy.concatenate() method.

Instead, you should call the numpy.concatenate() method with a tuple containing multiple arrays.

main.py
import numpy as np arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) arr3 = np.concatenate((arr1, arr2)) # ๐Ÿ‘‡๏ธ [1 2 3 4 5 6] print(arr3)

call numpy concatenate method with tuple containing multiple arrays

The code for this article is available on GitHub

Notice that we have 2 sets of parentheses in the call to numpy.concatenate().

The first argument the numpy.concatenate() method takes is a sequence of arrays.

The method joins the supplied sequence of arrays along an existing axis.

The arrays in the tuple must have the same shape, except in the dimension corresponding to the axis (the first axis (0) by default).

# Passing an array instead of a shape to numpy.ndindex()

Another cause of the error is when you pass an array instead of a shape to the numpy.ndindex() method.

Here is an example.

main.py
import numpy as np arr = np.array([ [1, 2], [3, 4], [5, 6], [7, 8] ]) # โ›”๏ธ TypeError: only integer scalar arrays can be converted to a scalar index print(np.ndindex(arr))

passing array instead of shape to numpy ndindex

The numpy.ndindex() method creates an N-dimensional iterator object to index arrays.

The method takes a shape as a parameter.

You can use the array.shape attribute to pass a shape to the method when calling it.

main.py
import numpy as np arr = np.array([ [1, 2], [3, 4], [5, 6], [7, 8] ]) # <numpy.ndindex object at 0x7f800758fa60> print(np.ndindex(arr.shape))

use array shape attribute

The code for this article is available on GitHub

The array.shape() method returns a tuple containing the array's dimensions.

main.py
import numpy as np arr = np.array([ [1, 2], [3, 4], [5, 6], [7, 8] ]) # (4, 2) print(arr.shape)

# 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