Fix – TypeError: only length-1 arrays can be converted to Python
Posted on: March 07, 2021 by Deven
If you are getting TypeError: only length-1 arrays can be converted to Python scalars error, this article will help you fix the issue.
Consider the following example which throws you the same error
import numpy as np
array1 = np.array(['apple', 'mango', 'orange', 'grapes'])
array2 = np.array(['potato', 'Tomato'])
array3 = np.concatenate(array1, array1)
print(array3)
Output of the above the Code.
Traceback (most recent call last):
File "main.py", line 7, in <module>
arraythree = np.concatenate(arrayone, arrayone)
TypeError: only length-1 arrays can be converted to Python scalars
To fix TypeError: only length-1 arrays can be converted to Python scalars issue you need to convert array 1 and array 2 into tuple or list.
Consider the example below:
# import numpy
import numpy
# Create array
array1 = numpy.array(['apple', 'mango', 'orange', 'grapes'])
array2 = numpy.array(['potato', 'Tomato'])
# Concatenate array arrray1& array2 by Tuple
array3 = numpy.concatenate((array1, array1))
print(array3)
output of the above code:
['apple' 'mango' 'orange' 'grapes' 'apple' 'mango' 'orange' 'grapes']
Share on social media
//