TypeError: 'numpy.ndarray' object is not callable in Python

Borislav Hadzhiev

Last updated: Apr 8, 2024

Reading timeยท9 min

- TypeError: 'numpy.ndarray' object is not callable in Python
- TypeError: 'Series' object is not callable in Python

If you got the error

"TypeError: 'Series' object is not callable", click on the second subheading.

**The Python "TypeError: 'numpy.ndarray' object is not callable" occurs when we
try to call an array as if it were a function or use the same name for a
variable and a function.**

**To solve the error, make sure to use square brackets when accessing an array
element at a specific index, e.g. my_array[0].**

Here is an example of how the error occurs.

main.py

`import numpy as np arr = np.array([1, 2, 3, 4]) # โ๏ธ TypeError: 'numpy.ndarray' object is not callable print(arr(1)) # ๐๏ธ using parentheses`

The issue is that we used parentheses `()`

instead of square brackets `[]`

when
accessing the
array at a
specific index.

To solve the error, use square brackets to access the array at an index.

main.py

`import numpy as np arr = np.array([1, 2, 3, 4]) # โ Now using square brackets to access an index print(arr[0]) # ๐๏ธ 1 print(arr[1]) # ๐๏ธ 2 print(arr[2]) # ๐๏ธ 3 print(arr[3]) # ๐๏ธ 4`

Python indexes are zero-based, so the first item in an array has an index of
`0`

, and the last item has an index of `-1`

or `len(arr) - 1`

.

The syntax for array slicing is `arr[start:stop:step]`

.

main.py

`import numpy as np arr = np.array([1, 2, 3, 4]) print(arr[0:2]) # ๐๏ธ [1 2] print(arr[1:3]) # ๐๏ธ [2 3]`

The `start`

index is inclusive and the `stop`

index is exclusive (up to, but not
including).

If the `start`

index is omitted, it is considered to be `0`

, if the `stop`

index
is omitted, the slice goes to the end of the list.

The error occurs for multiple reasons:

- Trying to access a numpy array with parentheses
`()`

instead of square brackets`[]`

. - Having a function and a variable with the same name.
- Overriding a built-in function by mistake and setting it to an array.
- Having a class method and a class property with the same name.
- Calling a function that returns a NumPy array twice.

Make sure you don't have a function and a variable with the same name.

main.py

`import numpy as np def example(): return 'bobbyhadz.com' example = np.array([1, 2, 3, 4]) # โ๏ธ TypeError: 'numpy.ndarray' object is not callable example()`

The `example`

variable shadows the function with the same name, so when we try
to call the function, we actually end up calling the variable.

Renaming the variable or the function solves the error.

main.py

`import numpy as np def example(): return 'bobbyhadz.com' arr = np.array([1, 2, 3, 4]) print(example()) # ๐๏ธ bobbyhadz.com`

We gave the variable a different name, so it no longer clashes with the function.

Now, we can call the function without any issues.

Make sure you aren't trying to call an array as a function in your code.

main.py

`import numpy as np arr = np.array([1, 2, 3, 4]) # โ๏ธ TypeError: 'numpy.ndarray' object is not callable arr() # ๐๏ธ remove parentheses`

The code sample tries to call the numpy array as a function.

To resolve the issue, remove the parentheses or correct the assignment.

main.py

`import numpy as np arr = np.array([1, 2, 3, 4]) print(arr) # ๐๏ธ [1 2 3 4]`

The error also occurs if we override the classes and methods from the `numpy`

module.

For example, don't import `array`

from NumPy and declare a variable named
`numpy`

as it would shadow the official class.

Here is another example of how the error occurs.

main.py

`import numpy as np list = np.array([1, 2, 3, 4]) # โ๏ธ TypeError: 'numpy.ndarray' object is not callable list('abc')`

We declared a `list`

variable and set it to a NumPy array.

The variable name clashes with the built-in `list`

function, so when we try to
call the function later on in our code, we actually call the NumPy array.

To solve the error, give the variable a different name that doesn't clash with built-in functions.

main.py

`import numpy as np a_list = np.array([1, 2, 3, 4]) print(list('abc')) # ๐๏ธ ['a', 'b', 'c']`

The variable no longer shadows a built-in function, so the issue is resolved.

Here is another example of how the error occurs.

main.py

`import numpy as np def get_array(): return np.array([1, 2, 3, 4]) # โ๏ธ TypeError: 'numpy.ndarray' object is not callable get_array()()`

Notice that we used two sets of parentheses when calling the `get_array`

function.

The first set of parentheses calls the function and the function returns a NumPy array.

The second set of parentheses calls the NumPy array and causes the error.

To solve the error, remove the second set of parentheses.

main.py

`import numpy as np def get_array(): return np.array([1, 2, 3, 4]) print(get_array()) # ๐๏ธ [1 2 3 4]`

Now we only call the function once and it returns a NumPy array.

If you are working with classes, make sure you don't have a method and a class property with the same name.

This causes the error in the same way as having a function and a variable with the same name does.

You have to rename the class method or the property, so they don't clash.

To solve the error, make sure:

- You aren't trying to access a NumPy array with parentheses
`()`

instead of square brackets`[]`

. - You don't have a function and a variable with the same name.
- You aren't overriding a built-in function and setting it to a NumPy array.
- You don't have a class method and a property with the same name.
- You aren't calling a function that returns a NumPy array twice.

**The Python "TypeError: 'Series' object is not callable" occurs when we try to
call a Series object as if it were a function.**

**To solve the error, resolve any clashes between function and variable names
and don't override built-in functions.**

Here is a very simple example of how the error occurs.

main.py

`import pandas as pd d = {'a': 1, 'b': 2, 'c': 3} ser = pd.Series(data=d, index=['a', 'b', 'c']) # โ๏ธ TypeError: 'Series' object is not callable print(ser())`

We are trying to call a pandas Series object as if it were a function.

If you added the parentheses by mistake, simply remove them to solve the error.

main.py

`import pandas as pd d = {'a': 1, 'b': 2, 'c': 3} ser = pd.Series(data=d, index=['a', 'b', 'c']) # a 1 # b 2 # c 3 # dtype: int64 print(ser)`

We removed the parentheses and we are no longer trying to call the `Series`

object as a function, so the issue is resolved.

The error occurs for multiple reasons:

- Trying to call a
`Series`

object with parentheses`()`

. - Having a function and a variable with the same name.
- Overriding a built-in function by mistake and setting it to a
`Series`

object. - Having a class method and a class property with the same name.
- Calling a function that returns a
`Series`

object twice.

Another common cause of the error is having a clash between function and variable names or overriding a built-in function.

main.py

`import pandas as pd d = {'a': 1, 'b': 2, 'c': 3} ser = pd.Series(data=d, index=['a', 'b', 'c']) # ๐๏ธ this overrides the built-in list() function list = ser # โ๏ธ TypeError: 'Series' object is not callable print(list(['a', 'b', 'c']))`

We declared a variable named `list`

and set it to a `Series`

object.

This declaration overrides the built-in

`list()`

constructor, so when we try to call the `list()`

constructor on the last line, we are actually calling the `Series`

object.To solve the error, rename the variable and not override any built-in methods.

main.py

`import pandas as pd d = {'a': 1, 'b': 2, 'c': 3} ser = pd.Series(data=d, index=['a', 'b', 'c']) # โ renamed variable my_list = ser print(list(['a', 'b', 'c']))`

The variable now has a different name and no longer shadows the built-in function.

The error also occurs if you have a function and a variable that share the same name.

main.py

`import pandas as pd d = {'a': 1, 'b': 2, 'c': 3} def series(): return 'bobbyhadz.com' series = pd.Series(data=d, index=['a', 'b', 'c']) # โ๏ธ TypeError: 'Series' object is not callable series()`

We defined a `series`

variable after defining a function with the same name.

When we try to call the `series`

function with parentheses, we are actually
calling the `Series`

object because the variable comes after the function.

To solve the error, rename your function or variable.

main.py

`import pandas as pd d = {'a': 1, 'b': 2, 'c': 3} def my_func(): return 'bobbyhadz.com' series = pd.Series(data=d, index=['a', 'b', 'c']) print(my_func()) # ๐๏ธ bobbyhadz.com`

Now the function and variable no longer share the same name, so the issue is resolved.

If you are working with classes, make sure you don't have a method and a class property with the same name.

This causes the error in the same way as having a function and a variable with the same name does.

You have to rename the class method or the property, so they don't clash.

Here is another example of how the error occurs.

main.py

`import pandas as pd def get_series(): d = {'a': 1, 'b': 2, 'c': 3} ser = pd.Series(data=d, index=['a', 'b', 'c']) return ser # โ๏ธ TypeError: 'Series' object is not callable get_series()()`

Notice that we used two sets of parentheses when calling the function.

The first set calls the function and the function returns a `Series`

object.

The second set of parentheses calls the `Series`

object and causes the error.

To resolve the error, remove the second set of parentheses.

main.py

`import pandas as pd def get_series(): d = {'a': 1, 'b': 2, 'c': 3} ser = pd.Series(data=d, index=['a', 'b', 'c']) return ser # a 1 # b 2 # c 3 # dtype: int64 print(get_series())`

A good way to start debugging is to `print(dir(your_object))`

and see what
attributes a `Series`

object has.

main.py

`import pandas as pd d = {'a': 1, 'b': 2, 'c': 3} ser = pd.Series(data=d, index=['a', 'b', 'c']) print(dir(ser))`

If you pass a class to the dir() function, it returns a list of names of the class's attributes, and recursively of the attributes of its bases.

You probably meant to access an attribute using dot notation instead of call the

`Series`

as a function.You can view the properties and methods a `Series`

object has in the
official pandas docs.

To solve the error, make sure:

- You aren't trying to call a
`Series`

object with parentheses. - You don't have a function and a variable with the same name.
- You aren't overriding a built-in function and setting it to a
`Series`

object. - You don't have a class method and a property with the same name.
- You aren't calling a function that returns a
`Series`

object twice.

You can learn more about the related topics by checking out the following tutorials:

- TypeError: Object of type ndarray is not JSON serializable
- ValueError: numpy.ndarray size changed, may indicate binary incompatibility
- NumPy RuntimeWarning: divide by zero encountered in log10
- ValueError: x and y must have same first dimension, but have shapes
- SystemError: initialization of _internal failed without raising an exception
- How to replace None with NaN in Pandas DataFrame
- ValueError: object too deep for desired array [Solved]
- Only one element tensors can be converted to Python scalars
- ValueError: cannot reshape array of size X into shape Y
- TypeError: Image data cannot be converted to float [Solved]
- OverflowError: Python int too large to convert to C long
- ValueError: DataFrame constructor not properly called [Fix]
- ufunc 'add' did not contain loop with signature matching types
- TypeError: Field elements must be 2- or 3-tuples, got 1
- ValueError: Expected 2D array, got 1D array instead [Fixed]
- ValueError: Found array with dim 3. Estimator expected 2
- Boolean index did not match indexed array along dimension 0
- Finding the Range of NumPy Array elements in Python
- Calculate the average (mean) of 2 NumPy arrays
- How to check if a NumPy Array is multidimensional or 1D
- Numpy: How to extract a Submatrix from an array
- Get N random Rows from a NumPy Array in Python
- How to remove Time from DateTime in Pandas [5 Ways]