Skip to main content

Python NumPy: How to Fix "TypeError: 'numpy.ndarray' object is not callable"

When working with NumPy, you might encounter the TypeError: 'numpy.ndarray' object is not callable. This error occurs when you use parentheses () on a variable that holds a NumPy array, treating it as if it were a function. In Python, the () syntax is the "call" operator, which is reserved for executing functions or methods, not for accessing elements in a data structure like an array.

The error is a clear signal that there is either a syntax mistake in how you are accessing array elements or a variable naming conflict in your code. This guide will walk you through the two most common causes of this error and show you how to resolve them.

Understanding the Error: The "Call" Operator ()

In Python, an object is "callable" if you can execute it using parentheses (). Functions and methods are callable. Data containers like NumPy arrays, lists, and tuples are not callable.

The TypeError: 'numpy.ndarray' object is not callable means Python has encountered code like my_array(...) and is telling you that the variable my_array contains a NumPy array, which is just data and cannot be executed as a function.

Cause 1: Using Parentheses () Instead of Square Brackets [] for Indexing

This is the most frequent cause of the error. The correct syntax for accessing an element in a NumPy array by its index is square brackets [], the same as with Python lists.

Example of code causing the error:

import numpy as np

arr = np.array([4, 5, 2, 3, 5, 4, 4, 9])

# Incorrect: Using parentheses () tries to "call" the array.
first_element = arr(0)

Output:

Traceback (most recent call last):
File "main.py", line 6, in <module>
first_element = arr(0)
TypeError: 'numpy.ndarray' object is not callable

Solution: to access array elements, you must use square brackets [] with the integer index of the element you want to retrieve.

import numpy as np

arr = np.array([4, 5, 2, 3, 5, 4, 4, 9])

# ✅ Correct: Use square brackets [] for indexing.
first_element = arr[0]
last_element = arr[-1]

print(f"The first element is: {first_element}")
print(f"The sum of the first and last elements is: {first_element + last_element}")

Output:

The first element is: 4
The sum of the first and last elements is: 13

Cause 2: Variable Name Shadowing a Function

This error can also occur if you accidentally name a variable with the same name as a function. This reassignment "shadows" or hides the original function. When you later try to call the function, you are instead trying to "call" your NumPy array variable.

Example of code causing the error:

import numpy as np

def greet():
print("Hello World!")

# ❌ Incorrect: The name 'greet' is reassigned to a NumPy array.
greet = np.array(['John', 'Lisa'])

# This now tries to call the NumPy array, not the function.
greet()

Output:

Traceback (most recent call last):
File "main.py", line 10, in <module>
greet()
TypeError: 'numpy.ndarray' object is not callable

Solution: always use unique and descriptive names for your variables and functions to avoid conflicts.

import numpy as np

def greet():
print("Hello World!")

# ✅ Correct: Use a different name for the array variable.
friends = np.array(['John', 'Lisa'])

# The original function remains accessible and can be called.
greet()
print(f"Friends array: {friends}")

Output:

Hello World!
Friends array: ['John' 'Lisa']
warning

Avoid Shadowing: It is a critical best practice in Python to avoid reusing names. This applies to your own functions as well as built-in functions like sum, max, or min.

Conclusion

The TypeError: 'numpy.ndarray' object is not callable is always caused by using the call operator () on a variable that contains a NumPy array. To fix it, you need to find where this is happening in your code:

If the cause is...The solution is...
Incorrectly trying to access an array element (e.g., my_array(0))Use the correct square bracket [] syntax for indexing: my_array[0].
A variable name that is the same as a function (e.g., my_func = np.array(...))Rename your variable to something unique to avoid shadowing the function.

By checking for these two common mistakes, you can easily diagnose and resolve this TypeError.