. Advertisement .
..3..
. Advertisement .
..4..
Please help me!
I get the “TypeError: only integer scalar arrays can be converted to a scalar index” in my below code which I attempt to concatenate two arrays of the same type and use 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
I would appreciate it if someone could tell me the cause of the error and how to fix it. Thanks.
The cause: Python numpy raises
typeerror: only integer scalar arrays can be converted to a scalar index
when you aim to transform the ordinary array into a scalar index. Another possible reason is when you try to concatenate but don’t pass a Tuple or a list.Solution:
Two ways to solve the problem:
Concatenate array by list
In the example below, It used the
numpy.concatenate()
method to convert array 1 and array 2 to a List. You must surround arrays 1 and 2 between square brackets.2. Concatenate array by Tuple
Array 1 and array 2 are converted to Tuple inside the
numpy.concatenate()
method.