How to Solve Python ‘numpy.ndarray’ object is not callable

by | Data Science, Programming, Python, Tips

numpy is a Python library for manipulating and numerical analysis of large, multi-dimensional arrays. Numpy’s N-dimensional arrays or ndarray is like any regular python array; you access its contents using indexing. To retrieve an item from a ndarray, you must use square brackets []. If you try to use parentheses () for the indexing operation, you will encounter the error” ‘numpy.ndarray’ object is not callable”.

This tutorial will go through the meaning of the error, and we will go through an example scenario of the error and learn how to solve it.


‘numpy.ndarray’ object is not callable

Callable objects in Python have the __call__ method. We call an object using parentheses. To verify if an object is callable, you can use the callable() built-in function and pass the object to it. If the function returns True, the object is callable, and if it returns False, the object is not callable.

Let’s test the callable() built-in function with a list of numbers:

numbers = [2, 3, 4]

print(callable(numbers))
False

The output tells us that lists are not callable.

Let’s test callable() on an example ndarray::

import numpy as np

numbers = np.array([1,2,3])

print(callable(numbers))
False

The error” ‘numpy.ndarray’ object is not callable” occurs when you try to call a numpy array as if it were a function to call. This error happens if you use round brackets () instead of square brackets [] to retrieve items from the array.

To solve this error, you must replace the () with [] when indexing.

Example: Accessing an Item in a Numpy Array

Let’s write a program that tells the user the ages of a selection of wines. We can use then calculate the average age for the wine selection.

Let’s start by storing the wine ages in years in a numpy array:

import numpy as np

wine_ages = np.array([25, 50, 100, 10, 40, 200])

To show a specific wine age and the average age, we can write the following code:

wine_age = wine_ages(3)

print(f'This wine is {wine_age} years old')

average_wine_age = np.sum(wine_ages)/wine_ages.size

print(f'The average wine age is {average_wine_age} years old')

First, we try to assign the fourth item to the variable wine_age and print it to the console. Then we calculate the average wine age and print it to the console. If we run the code, we get the following result:

TypeError                                 Traceback (most recent call last)
1 wine_age = wine_ages(3)

TypeError: 'numpy.ndarray' object is not callable

The error occurs because we tried to access the wine_ages array using parentheses instead of square brackets.

Solution

We must replace the round brackets with square brackets to solve this error.

wine_age = wine_ages[3]

print(f'This wine is {wine_age} years old')

average_wine_age = np.sum(wine_ages)/wine_ages.size

print(f'The average wine age is {np.round(average_wine_age)} years old')
This wine is 10 years old

The average wine age is 71.0 years old

We use the np.round() function to round up the average wine age to the nearest year. The code runs successfully, retries the fourth wine age, and calculates the average wine age.

Summary

Congratulations on reading to the end of this tutorial. The Python error “numpy.ndarray’ object is not callable'” occurs when using round brackets instead of square brackets to access an item from a numpy array. You can solve this error by using square brackets to access an item from the array. The Python syntax for indexing is

array_name[index_number]

For further reading on using numpy arrays go to the article: How-to Guide for Python NumPy Where Function.

For further reading on errors involving numpy arrays, go to the article: How to Solve Python ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().

Go to the online courses page on Python to learn more about coding in Python for data science and machine learning.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee