Last modified: Apr 25, 2026 By Alexander Williams
Fix TypeError: Only Length-1 Arrays
This error often stops beginners in their tracks. It appears when you try to use a NumPy array with a function that expects a single number. The message is clear: "only length-1 arrays can be converted to Python scalars."
Don't worry. This guide explains what causes the error and how to fix it. We will use short examples and simple code. By the end, you will handle this error with confidence.
What Does the Error Mean?
Python functions like int(), float(), math.sin(), or math.sqrt() expect a single scalar value. They do not accept arrays. When you pass a NumPy array with more than one element, Python cannot convert it. This triggers the TypeError.
For example, int([1, 2, 3]) fails because int() needs one number, not a list. The same happens with NumPy arrays.
Common Cause: Using Math Functions on Arrays
The most frequent cause is using Python's math module on a NumPy array. The math functions are designed for scalars only.
Here is a typical mistake:
import numpy as np
import math
# Create an array
arr = np.array([1, 2, 3])
# Wrong: math.sin expects a scalar
result = math.sin(arr)
Output:
TypeError: only length-1 arrays can be converted to Python scalars
The fix is simple. Use NumPy's own np.sin() instead. It works on arrays element-wise.
import numpy as np
arr = np.array([1, 2, 3])
result = np.sin(arr)
print(result)
Output:
[0.84147098 0.90929743 0.14112001]
Always prefer NumPy functions for arrays. This avoids the error entirely.
Another Cause: Converting Array to Scalar Directly
Sometimes you try to convert an array to a Python int or float. This only works if the array has exactly one element.
Example of failure:
import numpy as np
arr = np.array([10, 20])
value = int(arr) # Error: array has 2 elements
Output:
TypeError: only length-1 arrays can be converted to Python scalars
If you intend to get a scalar from a single-element array, use item() or index it.
import numpy as np
arr = np.array([10])
value = arr.item() # Works
print(value)
Output:
10
For multi-element arrays, use indexing to pick one element.
arr = np.array([10, 20, 30])
value = int(arr[0]) # Works: takes first element
print(value)
Output:
10
Using Custom Functions with Arrays
If you write a function that uses math or scalar operations, calling it on an array causes the error. The solution is to vectorize the function or use NumPy.
Example of a bad custom function:
import numpy as np
import math
def bad_func(x):
return math.sqrt(x) + 2
arr = np.array([4, 9, 16])
result = bad_func(arr) # Error
Output:
TypeError: only length-1 arrays can be converted to Python scalars
Fix it by using np.vectorize() or rewriting with NumPy.
import numpy as np
def good_func(x):
return np.sqrt(x) + 2
arr = np.array([4, 9, 16])
result = good_func(arr) # Works
print(result)
Output:
[4. 5. 6.]
Using np.vectorize() is another option:
import numpy as np
import math
def scalar_func(x):
return math.sqrt(x) + 2
vec_func = np.vectorize(scalar_func)
arr = np.array([4, 9, 16])
result = vec_func(arr)
print(result)
Output:
[4. 5. 6.]
Vectorize is slower than pure NumPy, so use it only when necessary.
Debugging Tips
When you see this error, check the following:
- Are you using
mathfunctions on an array? Switch tonpequivalents. - Are you trying to convert an array to
intorfloat? Ensure the array has one element or use indexing. - Are you calling a scalar-only function on an array? Vectorize or rewrite it.
Use type() and shape to inspect your variables.
import numpy as np
arr = np.array([5])
print(type(arr)) #
print(arr.shape) # (1,)
# Safe conversion
value = int(arr)
print(value) # Works because array has length 1
Output:
(1,)
5
Always check the array length before conversion.
How to Avoid This Error
Follow these best practices:
- Use NumPy functions instead of
mathfor array operations. - When you need a scalar, extract it with
item()or indexing. - Write functions that accept arrays by using NumPy internally.
- Test your code with small arrays first.
If you are new to NumPy, see our guide on Python TypeError: Causes and Fixes for broader context. This error is common but easy to fix once you understand the pattern.
Conclusion
The TypeError: only length-1 arrays can be converted to Python scalars happens when you mix scalar-only functions with NumPy arrays. The fix is always to use array-compatible functions or extract a single element. Remember to use np.sin instead of math.sin and item() instead of int(). With these changes, you can work with arrays smoothly.
For more debugging tips, check our article on Python TypeError: Causes and Fixes. Practice with small examples and you will master this error in no time.