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

Python numpy throws typeerror: only integer scalar arrays can be converted to a scalar index when you try to convert the ordinary array into a scalar index. The other cause might be when you try to concatenate and don’t pass Tuple or list for concatenation.

How to Fix only integer scalar arrays can be converted to a scalar index error?

Let’s consider the below code where we try to concatenate two arrays of the same type using numpy.concatenate() function.

# import numpy
import numpy

# Create 2 different arrays
ar1 = numpy.array(['Apple', 'Orange', 'Banana', 'Pineapple', 'Grapes'])
ar2 = numpy.array(['Onion', 'Potato'])

# Concatenate array ar1 & ar2 using numpy.concatenate()
ar3 = numpy.concatenate(ar1, ar2)
print(ar3)

# Output
Traceback (most recent call last):
  File "c:\Projects\Tryouts\listindexerror.py", line 9, in <module>
    ar3 = numpy.concatenate(ar1, ar1)
  File "<__array_function__ internals>", line 5, in concatenate
TypeError: only integer scalar arrays can be converted to a scalar index

By default, numpy can concatenate row-wise, and it requires either Tuple or list to concatenate. Since we are not passing either of them, Python will throw TypeError only integer scalar arrays can be converted to a scalar index.

There are two possible ways to fix the above issue.

Solution 1 – Concatenate array by list

If you look at the below example, we have converted array 1 and array 2 to List inside the numpy.concatenate() method. All you need to do is enclose array 1 and array 2 inside the square brackets.

# import numpy
import numpy

# Create 2 different arrays
ar1 = numpy.array(['Apple', 'Orange', 'Banana', 'Pineapple', 'Grapes'])
ar2 = numpy.array(['Onion', 'Potato'])

# Concatenate array ar1 & ar2 using numpy.concatenate()
ar3 = numpy.concatenate([ar1, ar2])
print(ar3)

# Output
['Apple' 'Orange' 'Banana' 'Pineapple' 'Grapes' 'Onion' 'Potato']

Solution 2 – Concatenate array by Tuple

If you look at the below example, we have converted array 1 and array 2 to Tuple inside the numpy.concatenate() method.

# import numpy
import numpy

# Create 2 different arrays
ar1 = numpy.array(['Apple', 'Orange', 'Banana', 'Pineapple', 'Grapes'])
ar2 = numpy.array(['Onion', 'Potato'])

# Concatenate array ar1 & ar2 using numpy.concatenate()
ar3 = numpy.concatenate((ar1, ar2))
print(ar3)

# Output
['Apple' 'Orange' 'Banana' 'Pineapple' 'Grapes' 'Onion' 'Potato']

The other possible reason you get the same error is when an ordinary list is indexed with a scalar index.

Example 

If you look at the below code, we pass the plain array and then perform the indexing operation where Python throws a type error.

import numpy as np
somelist = list(range(1000))
indices = np.random.choice(range(len(somelist)), replace=False, size=500)
print(somelist[indices.astype(int)])

# Output
Traceback (most recent call last):
  File "c:\Projects\Tryouts\listindexerror.py", line 4, in <module>
    print(somelist[indices.astype(int)])
TypeError: only integer scalar arrays can be converted to a scalar index

To fix the issue, you need to convert the ordinary error into a numpy array and then do the indexing operation below.

import numpy as np
somelist = list(range(1000))
indices = np.random.choice(range(len(somelist)), replace=False, size=500)
print(np.array(somelist)[indices.astype(int)])
1 comment
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Python String Split()

Python String split()

Table of Contents Hide split() Syntaxsplit() Parameterssplit() Return ValueExample 1: How split() works in Python?Example 2: How split() works when maxsplit is specified? The Python String split() method is a…
View Post
Python Comment Block / Python Multiline Comment

Python Comment Block

Table of Contents Hide Introduction to Python Comment BlockTypes of comments in PythonSingle-line commentsInline commentsMultiline commentsUsing Multiple Hashtags (#)Python docstringsSingle line docstringsMulti-line docstrings Comments are a piece of text in…
View Post
Python String Upper()

Python String upper()

Table of Contents Hide upper() Syntaxupper() Parameterupper() Return ValueExample 1: Convert a string to uppercaseExample 2: How upper() is used in a program?Example 3: How to check if two strings…
View Post