While dealing with NumPy arrays, we may face TypeError: only size-1 arrays can be converted to Python scalars error which occurs when we pass in an array to a method that accepts only one parameter. There can be other many possible reasons as well. In this short article, we will learn how we can solve TypeError: only size-1 arrays can be converted to Python scalars error using various methods. Moreover, we will also discuss the reasons for getting this error.
Solve TypeError: only size-1 arrays can be converted to Python scalars
We know that the error occurs because we are trying to pass in an array to a method that can only accept one parameter. Let us assume that we have the following function and we are passing an array to it:
# importing numpy array
import numpy as np
# creating numpy array
y = np.array([1, 2, 3, 4])
# passing numpy array to int method
x = np.int(y)
Output:

As you can see, we get an error because we have passed a NumPy array to int() method which can only take a single value.
Now let us solve this error using the following methods:
- Using vectoirzed() method
- Using map() function
- Using astype() method
- Using For loop
- Using apply_along_axis() method
Now let us jump into each of these methods and explain each of them with examples.
Using vectorized() method
Similar to the python map function, the vectorized function evaluates pyfunc over the input arrays’ subsequent tuples while utilizing numpy’s broadcasting principles. By running the function with the first input element, vectorized may determine the data type of the output.
Let us use the vectorized() method which will help us to get rid of TypeError: only size-1 arrays can be converted to Python scalars error:
# importing numpy array
import numpy as np
# creating numpy array
y = np.array([1.5, 2.5, 3.3, 4.9])
# using vectorize method
v = np.vectorize(np.int_)
# passing numpy array to int method
x = v(y)
# printing
print(x)
Output:
[1 2 3 4]
As you can see, we converted the array to an integer successfully without getting errors by using the vectorized() method.
Using the map() function
It is possible to process and transform all the elements in an iterable without using a for loop explicitly in Python using the built-in method map(), which is also known as mapping. When you need to apply a transformation function to every item in an iterable and turn them into a new iterable, map() comes in handy.
Now let us see, how we can use the map() method to get rid of TypeError: only size-1 arrays can be converted to Python scalars error.
# importing numpy array
import numpy as np
# creating numpy array
y = np.array([1.5, 2.5, 3.3, 4.9])
# passing numpy array to int method
x = np.array(list(map(np.int_, y)))
# printing
print(x)
Output:
[1 2 3 4]
As you can see, we were able to convert the NumPy array to int without getting an error. We use the map function to map each element from the array.
Using astype() method
The astype() method returns a new DataFrame with the provided type of data as the default. You can either use a Python dictionary to specify a data type for each column or cast the entire DataFrame to a single particular data type, like in the following example: “Duration”: “int64,” “Pulse”: “float,” and “Calories”: “int64”.
Let us now use the astype() method to solve the problem:
# importing numpy array
import numpy as np
# creating numpy array
y = np.array([1.5, 2.5, 3.3, 4.9])
# passing numpy array to int method
x = y.astype(int)
# printing
print(x)
Output:
[1 2 3 4]
As you can see, we didn’t get the TypeError: only size-1 arrays can be converted to Python scalars error.
Using for loop
We can also use the for loop to iterate through the NumPy array and convert each element separately. See the example below:
# importing numpy array
import numpy as np
# creating numpy array
y = np.array([1.5, 2.5, 3.3, 4.9])
# creating empty array
x = np.array([None]*len(y))
# using for loop
for i in range(len(y)):
x[i]= np.int(y[i])
# printing
print(x)
Output:
[1 2 3 4]
As you can see, we have successfully converted the elements of the array without getting errors.
Using apply_along_axis() method
Now we will use the lambda function to create a function that will convert the elements of the array to different datatype and then will apply the method along axis of the array.
# importing the numpy module
import numpy as np
# creating numpy array
x = np.array([1.5, 2.5, 3.3, 4.9])
# using lambda function
app = lambda y: [np.int(i) for i in y]
# applying function
x = np.apply_along_axis(app, 0, x)
print(x)
Output:
[1 2 3 4]
As you can see, now we were able to change the data type of the elements in an array without getting an error.
Understading TypeError: only size-1 arrays can be converted to Python scalars
In Python, the error gives a lot of information. The first part of the error shows the category of the error which in this case is the TypeError. The second part gives more information and explain the error.
What is TypeError in Python?
When an object’s data type during an operation is incorrect, the Python TypeError exception is thrown. This can occur when an operation is carried out on an object that is either not supported for the operation or of the improper type.
What is an array in Python?
A fundamental data structure, arrays play a significant role in the majority of programming languages. They are containers in Python that can hold many items simultaneously. They are a set of items that are organized and whose values all belong to the same data type.
Summary
In this short article, we discussed how we can solve TypeError: only size-1 arrays can be converted to Python scalars error. We learned what the error actually means and how we understand errors in Python. We came across five different methods to solve the TypeError: only size-1 arrays can be converted to Python scalars error.
Related Errors
- IndentationError: Unindent Does Not Match Any Outer Indentation Level
- Install Pandas on Jupyter Notebook in 4 Ways
- [Solved] Attributeerror: module matplotlib has no attribute subplots
- AttributeError: ARIMAResults Object Has No Attribute plot_predict
- Reverse a String in Python Using 7 Methods