[Solved] NumPy.ndarray object is Not Callable Python

In Python, the array will be accessed using an indexing method. Similarly, the NumPy array also needs to be accessed through the indexing method. In this article, we will look at how to fix the NumPy.ndarray object is Not Callable error and what causes this error in the first place.

NumPy.ndarray object is Not Callable Error

The object is not callable error occurs when you try to access the NumPy array as a function using the round brackets () instead of square brackets [] to retrieve the array elements.

In Python, the round brackets or parenthesis () denotes a function call, whereas the square bracket [] denotes indexing. Hence, when using round brackets while accessing the array, Python cannot handle it and throws an error.

An Example

Let’s take a simple example, we have an array of fruits, and we are trying to access the last element of an array and print the fruit.

# NumPy.ndarray object is Not Callable Error
import numpy as np

fruits = np.array(["Apple","Grapes","WaterMelon","Orange","Kiwi"])
last_fruit = fruits(-1)

print("The last fruit in the array is {} ".format(last_fruit))

When we run the code, we get an error, as shown below.

Traceback (most recent call last):
  File "c:/Projects/Tryouts/main.py", line 5, in <module>
    last_fruit = fruits(-1)
TypeError: 'numpy.ndarray' object is not callable

Solution NumPy.ndarray object is Not Callable Error

In the above example, we tried to access the last item of the array element using the round brackets (), and we got an object is not Callable Error.

fruits = np.array(["Apple","Grapes","WaterMelon","Orange","Kiwi"])
last_fruit = fruits(-1)

We can fix this code by replacing the round brackets with square brackets, as shown below.

import numpy as np

fruits = np.array(["Apple","Grapes","WaterMelon","Orange","Kiwi"])
last_fruit = fruits[-1]

print("The last fruit in the array is {} ".format(last_fruit))

Output

The last fruit in the array is Kiwi 

Conclusion

The ‘numpy.ndarray’ object is not callable error occurs when you try to access the NumPy array as a function using the round brackets () instead of square brackets [] to retrieve the array elements. To fix this issue, use an array indexer with square brackets to access the elements of the array.

Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
List of Python free ebooks

List of Free Python Books

Table of Contents Hide Python SuccinctlyPython Data Science HandbookThink Python 2nd EditionAutomate the Boring Stuff with PythonMaking Games with Python & PygameData Structures and Algorithms in Python Google Python Style GuideA…
View Post