Register Login

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

Updated Jan 08, 2020

Only integer scalar arrays can be converted to a scalar index

In the following example we are trying to concatenate two arrays using NumPy’s concatenate function, the concatenate function concatenating two or more arrays of the same type.

It usually can concatenate row-wise and column-wise.

By default NumPy’s concatenate function concatenate row-wise, to do so it requires iterable (tuple or list) to concatenate.

Example:

# import numpy
import numpy

# Create array
ar1 = numpy.array(['Red', 'Blue', 'Green', 'Orange'])
ar2 = numpy.array(['Black', 'Yellow'])

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

Output:

Traceback (most recent call last):
  File "error-1.py", line 9, in <module>
    ar3 = numpy.concatenate(ar1, ar1)
  File "<__array_function__ internals>", line 6, in concatenate
TypeError: only integer scalar arrays can be converted to a scalar index

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

Solution:

To solve this error you need to convert array 1 and array 2 in to tuple or list. Kindly see the following example for better understanding.

Example: Concatenate array by Tuple

# import numpy
import numpy

# Create array
ar1 = numpy.array(['Red', 'Blue', 'Green', 'Orange'])
ar2 = numpy.array(['Black', 'Yellow'])

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

Output:

['Red' 'Blue' 'Green' 'Orange' 'Red' 'Blue' 'Green' 'Orange']

Example: Concatenate array by List

# import numpy
import numpy

# Create array
ar1 = numpy.array(['Red', 'Blue', 'Green', 'Orange'])
ar2 = numpy.array(['Black', 'Yellow'])

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

Output:


['Red' 'Blue' 'Green' 'Orange' 'Red' 'Blue' 'Green' 'Orange']

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


×