How to fix TypeError: ‘numpy.ndarray’ object is not callable ?

We see the TypeError: ‘numpy.ndarray’ object is not callable error in Python when we try to index the numpy.ndarray object wrongly. This is a very simple error and happens when we have just started using Numpy Array without properly knowing how to index these arrays.

In this article, let’s take a look at what causes this error and determine how to overcome this error.

Also check, How to print the full NumPy array without any truncation in the output?

What causes TypeError: ‘numpy.ndarray’ object is not callable?

In Python, we call a function using the round brackets (). To index, we use the square brackets []. When we try to index the array using the round brackets, Python thinks of it as a function call. Because it doesn’t find any corresponding function definition for it, it raises an error ‘numpy.ndarray’ object is not callable

 

TypeError: 'numpy.ndarray' object is not callable- right usage

 

Let’s have a look at an example.

import numpy as np

arr=np.array([1,2,3,4])
print("First element is",arr(0))

 

Output:

Traceback (most recent call last):
  File "C:\Users\paian\PycharmProjects\PythonHowTo\venv\Solutions\Print Numpy Array without truncation.py", line 4, in <module>
    print("First element is",arr(0))
TypeError: 'numpy.ndarray' object is not callable

 

Explanation:

We see an error in this case, as we’re trying to access the array using the arr(0). Python thinks of it as a function call. Because arr is a numpy.ndarray object and it does not have callable property, it throws the object is not callable error.

How to Fix TypeError: ‘numpy.ndarray’ object is not callable ?

To fix this error, use the right indexing notation that is square brackets instead of round brackets as shown below.

import numpy as np

arr=np.array([1,2,3,4])
print("First element is",arr[0])

Output:

First element is 1

Conclusion:

In this short article, we have discovered TypeError: ‘numpy.ndarray’ object is not callable is seen when we try to index the NumPy array using the () instead of []. To avoid this error, we can use the notation, numpyarray[index] to index the arrays. We hope this article has been informative.

 

If you enjoyed reading, share this article.

Anusha Pai is a Software Engineer having a long experience in the IT industry and having a passion to write. She has a keen interest in writing Python Errorfixes, Solutions, and Tutorials.

Leave a Comment