[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.

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Numpy Repeat

NumPy Repeat

Table of Contents Hide NumPy Repeat SyntaxNumPy Repeat ParametersNumPy Repeat Return ValueExample 1: Repeating the Single Dimensional NumPy ArrayExample 2: Repeating the Two-Dimensional NumPy ArrayExample 3: Repeating the NumPy Array…
View Post
Python String Lstrip()

Python String lstrip()

Table of Contents Hide lstrip() Syntaxlstrip() Parameterslstrip() Return ValueExample 1: Working of lstrip()Example 2 – How to use lstrip() method in real world? The Python String lstrip() method is a…
View Post
Python String Rindex()

Python String rindex()

Table of Contents Hide rindex() Syntaxrindex() Parametersrindex() Return ValueDifference between rindex() method and rfind() methodExample 1: Find the last occurence of a string in PythonExample 2: If string is not…
View Post