DEV Community

Srinivas Ramakrishna for ItsMyCode

Posted on • Originally published at itsmycode.com on

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

ItsMyCode |

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
Enter fullscreen mode Exit fullscreen mode

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']
Enter fullscreen mode Exit fullscreen mode

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']
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)])
Enter fullscreen mode Exit fullscreen mode

The post TypeError: only integer scalar arrays can be converted to a scalar index appeared first on ItsMyCode.

Top comments (0)