If you try to index a Python list with an array of integer values you will raise the error: TypeError: only integer scalar arrays can be converted to a scalar index. You can only index an ordinary Python list with single integer values. If you want to use an array of indices you must convert the list to a NumPy array using the function numpy.array().

This error can also occur if you try to concatenate NumPy arrays but you do not pass the arrays in a tuple or a list.

This tutorial will go through the error in detail and examples to learn how to solve it.


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

What is a TypeError?

TypeError tells us that we are trying to perform an illegal operation for a specific Python data type. Specifically, indexing with an array of values is an illegal operation for the Python data type: List.

Indexing NumPy Arrays

We can access NumPy arrays just like lists with the following syntax:

array[start:stop:step]

However, we can also get a subset of a NumPy array using integer index arrays. For example, if we want to access the first, third, and fifth elements of a one-dimensional array:

import numpy as np

arr = np.array([10, 20, 30, 40, 50, 60], int)

selection = np.array([0, 2, 4], int)

print(arr[selection])
[10 30 50]

However, we cannot do this with lists, we can only use an integer value to index a list.

Example: Trying to Index a Python List Like a NumPy Array

Let’s look at an example of trying to index a Python list like a NumPy array. We will create a list containing all numbers from 0 to 5000 and then create an array of fifty indices that are selected at random from the numbers between 0 and the length of the list. We will then attempt to print the values in the list at the given indices. The code looks as follows:

a_list = list(range(5000))

indices = np.random.choice(range(len(a_list)), replace=False, size=50)

print(a_list[indices.astype(int)])

Let’s run the code to see what happens:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
1 print(a_list[indices.astype(int)])

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

We get the TypeError because we are trying to index a Python list with an array, which is not a legal operation for the list data type.

Solution: Convert Ordinary Python List to NumPy Array

To solve this error we need to convert the list to a NumPy array. Once we have a NumPy array we can obtain a subset of its elements using an array of indices. Let’s look at the revised code:

a_list = list(range(5000))

indices = np.random.choice(range(len(a_list)), replace=False, size=50)

print(np.array(a_list)[indices.astype(int)])

To convert the list to an array we use the function np.array(). Let’s run the code to see the result:

[3702   34 2698 2406  821 1883 3206  737 4680  608 4972 1867 3231 1537
  914 4144 4180 2423 1258 4000 2025 3756  122 2889  530 3550  260 3788
  187 4393 2549 2890 4797 4706 3931  656  974 2972 4331 4672 1379 1521
  258 3753 3477  582 3211 2837 4430 4769]

We successfully obtained fifty random elements from the list after converting the list to a NumPy array.

Example: Concatenating to NumPy Arrays

The error can also occur when you try to concatenate two NumPy arrays incorrectly. Let’s look at an example of concatenating two NumPy arrays.

import numpy as np

arr_1 = np.array(['Jupiter', 'Mars', 'Neptune', 'Mercury'])

arr_2 = np.array(['Earth', 'Venus', 'Uranus', 'Saturn'])

arr_3 = np.concatenate(arr_1, arr_2)

print(arr_3)

In the above program, we use the np.concatenate() function on the two arrays, where np is shorthand for numpy. Let’s run the code to see what happens:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
      3 arr_2 = np.array(['Earth', 'Venus', 'Uranus', 'Saturn'])
      4 
      5 arr_3 = np.concatenate(arr_1, arr_2)

__array_function__ internals in concatenate(*args, **kwargs)

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

Our program throws the TypeError because you have to concatenate numpy arrays within a tuple or a list.

Solution #1: Concatentate NumPy Arrays by Tuple

To correctly concatenate NumPy arrays you can put them in a tuple, which involves wrapping them in parentheses and using a comma to separate them. Let’s look at the revised code:

import numpy as np

arr_1 = np.array(['Jupiter', 'Mars', 'Neptune', 'Mercury'])

arr_2 = np.array(['Earth', 'Venus', 'Uranus', 'Saturn'])

arr_3 = np.concatenate((arr_1, arr_2))

print(arr_3)

Let’s run the code to see what happens:

['Jupiter' 'Mars' 'Neptune' 'Mercury' 'Earth' 'Venus' 'Uranus' 'Saturn']

We successfully concatenated the two arrays and printed the new array to the console.

Solution #2: Concatenate NumPy Arrays by List

We can also concatenate NumPy arrays by putting them in a list, which involves wrapping them in square brackets and using a comma to separate them. Let’s look at the revised code:

import numpy as np

arr_1 = np.array(['Jupiter', 'Mars', 'Neptune', 'Mercury'])

arr_2 = np.array(['Earth', 'Venus', 'Uranus', 'Saturn'])

arr_3 = np.concatenate([arr_1, arr_2])

print(arr_3)

Let’s run the code to see what happens:

['Jupiter' 'Mars' 'Neptune' 'Mercury' 'Earth' 'Venus' 'Uranus' 'Saturn']

We successfully concatenate the two arrays and printed the new array to the console.

Summary

Congratulations on reading to the end of this tutorial! The error ‘TypeError: only integer scalar arrays can be converted to a scalar index’ occurs when you try to index an ordinary Python list with an array of values. If you want to index a list, you have to use single values. If you want to index with an array of values must convert the list to a NumPy array using the function numpy.array(). The error can also occur if you concatenate NumPy without passing the arrays as a tuple or a list. Ensure that you pass the arrays in a tuple or a list when performing the concatenation operation.

For further reading on errors involving NumPy arrays and scalar values, go to the article: How to Solve Python TypeError: only size-1 arrays can be converted to Python scalars.

Go to the online courses page on Python to learn more about coding in Python for data science and machine learning.

Have fun and happy researching!