How do I fix Python: ‘numpy.ndarray’ object is not callable

This error occurs when you are trying to call a numpy array as a function. To fix this, you need to make sure you are using the numpy array correctly. For example, you may be trying to call a function on the array instead of using the correct syntax for the array. If needed, you can also look up the documentation for the numpy array to make sure you are using it correctly.


One common error you may encounter when using NumPy in Python is:

TypeError: 'numpy.ndarray' object is not callable

This error usually occurs when you attempt to call a NumPy array as a function by using round () brackets instead of square [ ] brackets.

The following example shows how to use this syntax in practice.

How to Reproduce the Error

Suppose we have the following NumPy array:

import numpy as np

#create NumPy array
x = np.array([2, 4, 4, 5, 9, 12, 14, 17, 18, 20, 22, 25])

Now suppose we attempt to access the first element in the array:

#attempt to access the first element in the array
x(0)

TypeError: 'numpy.ndarray' object is not callable

Since we used round () brackets Python thinks we’re attempting to call the NumPy array x as a function.

Since x is not a function, we receive an error.

How to Fix the Error

The way to resolve this error is to simply use square [ ] brackets when accessing elements of the NumPy array instead of round () brackets:

#access the first element in the array
x[0]

2

The first element in the array (2) is shown and we don’t receive any error because we used square [ ] brackets.

Also note that we can access multiple elements of the array at once as long as we use square [ ] brackets:

#find sum of first three elements in array
x[0] + x[1] + x[2]

10

The following tutorials explain how to fix other common errors in Python:

x