TypeError: Only size-1 arrays can be converted to Python scalars

To solve TypeError: Only size-1 arrays can be converted to Python scalars, use the np.vectorize() function. The numpy vectorize() is a library function that takes a nested sequence of objects or numpy arrays as inputs and returns a single numpy array or a tuple of numpy arrays.

The TypeError is raised when a function expects a single value, but the user passes an array of values. It occurs mainly when we pass the wrong data type into a function. For example, this TypeError is raised using Python’s “Numpy” package. The user must pass appropriate parameters to the function to avoid this TypeError.

TypeError: size-1 arrays can be converted to Python scalars error is raised

import numpy as np


def fun(a):
 return np.int32(a)


a = np.arange(1, 10.5, 0.1)
print(fun(a))

Output

TypeError: only size-1 arrays can be converted to Python scalars

You can see that the above code raises a TypeError known as the only size-1 arrays that can be converted to Python scalars. This is because the np.arange() method is a numpy method that creates an array filled with a range of values in Python.

One is the starting index, 10.5 is the stopping index, and 0.1 is the step. This error is caused because we pass the float array into a function and convert that float array into an int.

We can convert a float number into an int, but we cannot convert an array into an int. That’s why this TypeError is raised.

There are mainly two ways to solve the TypeError problem programmatically.

  1. Using np.vectorize() function
  2. Using astype() function

Using numpy vectorize()

To solve a TypeError: Only size-1 arrays can be converted to Python scalars, use the np.vectorize() function. It is similar to for loop, but it cannot be used when large values are in an array.

import numpy as np


def fun(a):
 return np.int32(a)


f = np.vectorize(fun)
a = np.arange(1, 10.5, 0.1)
print(f(a))

Output

[ 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3
3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5
5 5 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 8 8
8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 10 10 10 10 10]

You can see that the TypeError is solved, and we get the final output without any error or warning.

Using astype() function

The astype() is a built-in numpy library function that converts all the elements into the type given as a parameter. 

import numpy as np

arr = np.arange(1, 10.5, 0.1)
a = arr.astype(int)
print(a)

Output

[ 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3
3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5
5 5 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 8 8
8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 10 10 10 10 10]

Until now, we have used two numpy functions for solving this TypeError. However, there is a simple way to solve this error without using built-in functions. We can use just the for loop for solving this TypeError.

import numpy as np

arr = np.arange(1, 10.5, 0.1)
op = [int(i) for i in arr]
print(op)

Output

[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10]

This program uses it to iterate over all the elements in the array, convert them into an int, and store them in another array. This is the simplest way to solve this error. In this type, I have used generators for solving it.

Generators are similar to for loop, but we write them in single lines instead of several lines. This program can also be written by:

import numpy as np

a = np.arange(1, 10.5, 0.1)
for i in range(len(a)):
a[i] = np.int32(a[i])
print(a)

Output

[ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 2. 2. 2. 2. 2. 2. 2. 2.
2. 2. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 4. 4. 4. 4. 4. 4.
4. 4. 4. 4. 5. 5. 5. 5. 5. 5. 5. 5. 5. 5. 6. 6. 6. 6.
6. 6. 6. 6. 6. 6. 7. 7. 7. 7. 7. 7. 7. 7. 7. 7. 8. 8.
8. 8. 8. 8. 8. 8. 8. 8. 9. 9. 9. 9. 9. 9. 9. 9. 9. 9.
10. 10. 10. 10. 10.]

We can also use the Python map() function to solve this TypeError. The map() is a built-in function similar to for loop as it can be used to assign the values in a list.

import numpy as np

arr = np.arange(1, 10.5, 0.1)
a = np.array(list(map(np.int32, arr)))
print(a)

Output

[ 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3
3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5
5 5 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 8 8
8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 10 10 10 10 10]

Conclusion

We used many methods to solve TypeError: Only size-1 arrays can be converted to Python scalars. If you ask for my recommendation, I suggest you use the np.vectorize() or astype() method to solve this TypeError because it is easy and understandable. It is also recommended that, based on your use case, you can use one of these methods.

That’s it for this tutorial.

Leave a Comment