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

In this short tutorial, we look at the typeerror "Only size-1 arrays can be converted to Python scalars" in Python. This typeerror is quite common however there are only a handful of tutorials explaining it. In case you are looking for an in-depth explanation of the error I would recommend you follow step by step. If not, feel free to check out the solutions directly.

This tutorial is a part of our initiative at Flexiple, to write short curated tutorials around often used or interesting concepts.

Table of Contents - Typeerror: only size-1 arrays can be converted to Python scalars

What does "Only size-1 arrays can be converted to Python scalars" signify?

You are likely to face the above typeerror while working with NumPy and matplotlib.pyplot. This error arises when you pass an array into a function that is expecting a single value (i.e., scalar value). Python generally works with only a handful of scalar values (Int, float, bool, etc) however while dealing with NumPy we have another 24 new fundamental Python types to describe different types of scalars. You can read more about them here.

Due to the above errors, you ought to be extra cautious while using NumPy. A common error I came across was this:

import numpy as ny
import matplotlib.pyplot as matplot

def ycord(xcord):
    return ny.int(xcord)

xcord = ny.arange(1, 5, 2.7)
matplot.plot(xcord, ycord(xcord))
matplot.show()

For the above input, this is the output we receive:

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

This is because the .int function only accepts single values and in our case we are trying to pass x which is an array.

Solution 1: Code and Explanation

In the first method, we use the .vectorize function. The .vectorize function is essentially a for loop that takes in an array and returns a single numpy array. Using this method we iterate over the array x and return a single value. Thus we do not encounter the "only size-1 arrays can be converted to Python scalars" typeerror.

import numpy as ny
import matplotlib.pyplot as matplot
xcord = ny.arange(1, 5, 2.5)
ycord = ny.vectorize(ny.int)
matplot.plot(xcord, ycord(xcord))
matplot.show()

As you can see we no longer need to define a function as we have used the .int function inside the .vectorize function, which loops through the array and returns a single array that is accepted by the .int function.

Solution 2: Code and Explanation

Another method I came across utilizes the .astype method, this method casts the array into a specified type in our case int. Although this method works, I personally recommend the first solution as this method casts a string into an int which is not a desirable method.

import numpy as ny
import matplotlib.pyplot as matplot

def ycord(xcord):
    return xcord.astype(int)

xcord = ny.arange(1, 5, 2.5)
matplot.plot(xcord, ycord(xcord))
matplot.show()

These are two solutions that can be used when faced with the Typeerror: Only size-1 arrays can be converted to Python scalars error.

Do let me know your thoughts/ queries in the comment sections below. :)