Fix – TypeError: only length-1 arrays can be converted to Python

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…

The post Fix – TypeError: only length-1 arrays can be converted to Python appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored 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']

The post Fix – TypeError: only length-1 arrays can be converted to Python appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Deven


Print Share Comment Cite Upload Translate Updates
APA

Deven | Sciencx (2021-03-07T05:29:55+00:00) Fix – TypeError: only length-1 arrays can be converted to Python. Retrieved from https://www.scien.cx/2021/03/07/fix-typeerror-only-length-1-arrays-can-be-converted-to-python/

MLA
" » Fix – TypeError: only length-1 arrays can be converted to Python." Deven | Sciencx - Sunday March 7, 2021, https://www.scien.cx/2021/03/07/fix-typeerror-only-length-1-arrays-can-be-converted-to-python/
HARVARD
Deven | Sciencx Sunday March 7, 2021 » Fix – TypeError: only length-1 arrays can be converted to Python., viewed ,<https://www.scien.cx/2021/03/07/fix-typeerror-only-length-1-arrays-can-be-converted-to-python/>
VANCOUVER
Deven | Sciencx - » Fix – TypeError: only length-1 arrays can be converted to Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/03/07/fix-typeerror-only-length-1-arrays-can-be-converted-to-python/
CHICAGO
" » Fix – TypeError: only length-1 arrays can be converted to Python." Deven | Sciencx - Accessed . https://www.scien.cx/2021/03/07/fix-typeerror-only-length-1-arrays-can-be-converted-to-python/
IEEE
" » Fix – TypeError: only length-1 arrays can be converted to Python." Deven | Sciencx [Online]. Available: https://www.scien.cx/2021/03/07/fix-typeerror-only-length-1-arrays-can-be-converted-to-python/. [Accessed: ]
rf:citation
» Fix – TypeError: only length-1 arrays can be converted to Python | Deven | Sciencx | https://www.scien.cx/2021/03/07/fix-typeerror-only-length-1-arrays-can-be-converted-to-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.