TypeError: 'Series' object is not callable in Python

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

TypeError: 'Series' object is not callable in Python #

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.

typeerror series object is not callable

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 a 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)

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 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, you have to 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']))

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.

Conclusion #

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.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.