How to Solve Python TypeError: ‘Series’ object is not callable

by | Programming, Python, Tips

The TypeError ‘Series’ object is not callable occurs when you try to call a Series object by putting parentheses () after it like a function. Only functions respond to function calls.

You can solve this error by using square brackets to access values in a Series object. For example,

import pandas as pd

vals = {'x': 73 , 'y': 21, 'z': 10}

ser = pd.Series(data=vals)

print(ser['x'])

This tutorial will go through the error in detail and how to solve it with the help of code examples.


TypeError: ‘Series’ object is not callable

Calling a function means the Python interpreter executes the code inside the function. In Python, we can only call functions. We can call functions by specifying the name of the function we want to use followed by a set of parentheses, for example, function_name(). Let’s look at an example of a working function that returns a string.

# Declare function

def simple_function():

    print("Learning Python is fun!")

# Call function

simple_function()
Learning Python is fun!

We declare a function called simple_function in the code, which prints a string. We can then call the function, and the Python interpreter executes the code inside simple_function().

Series objects do not respond to a function call because they are not functions. If you try to call a Series object as if it were a function, you will raise the TypeError: ‘Series’ object is not callable.

We can check if an object is callable by passing it to the built-in callable() method. If the method returns True, then the object is callable. Otherwise, if it returns False the object is not callable. Let’s look at evaluating a Series object with the callable method:

import pandas as pd

vals = {'x': 73 , 'y': 21, 'z': 10}

ser = pd.Series(data=vals)

print(callable(ser))
False

The callable function returns False for the Series object.

Example #1

Let’s look at an example of attempting to call a Series object. First, we will import pandas and then create a Series object from a dictionary containing pizza names as keys and pizza prices as values.

import pandas as pd

pizzas = {'margherita': 10.99 , 'pepperoni': 11.99, 'marinara': 7.99}

ser = pd.Series(data=pizzas)

Next, we will try to access the row with the index ‘marinara‘.

print(ser('marinara'))

Let’s run the code to see what happens:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [17], in <cell line: 7>()
      3 pizzas = {'margherita': 10.99 , 'pepperoni': 11.99, 'marinara': 7.99}
      5 ser = pd.Series(data=pizzas)
----> 7 print(ser('marinara'))

TypeError: 'Series' object is not callable

The error occurs because we tried to access the row with the index ‘marinara‘ using parentheses. Putting parentheses after the Series object is interpreted by Python as a function call.

Solution #1: Use square brackets

To solve this error, we can access the row of the Series object using square brackets. Let’s look at the revised code:

import pandas as pd

pizzas = {'margherita': 10.99 , 'pepperoni': 11.99, 'marinara': 7.99}

ser = pd.Series(data=pizzas)

print(ser['marinara'])

print(type(ser['marinara']))

Let’s run the code to see the result:

7.99
<class 'numpy.float64'>

The above value is a numpy.float64 containing the price of the marinara pizza.

Solution #2: Use dot notation

We can also use the dot notation to access the attributes of the Series object. We can use the dir() method to list the attributes of the object:

Let’s look at the revised code:

import pandas as pd

pizzas = {'margherita': 10.99 , 'pepperoni': 11.99, 'marinara': 7.99}

ser = pd.Series(data=pizzas)

print(ser.marinara])

We used the dot notation to access the marinara row of the Series object. Let’s run the code to get the result:

7.99

Example #2: Reassigning a Reserved Name

The error can also occur if we reassign a reserved name for a built-in function like list() to Series object.

Let’s look at an example:

import pandas as pd

pizzas = {'margherita': 10.99 , 'pepperoni': 11.99, 'marinara': 7.99}

list = pd.Series(data=pizzas)

a_set = {2, 4, 6}

list(a_set)

In the above code, we defined a Series object and then assigned it to the variable name list. We then define a set of integers and try to convert it to a list using the built-in list() method. Let’s run the code to see what happens:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [22], in <cell line: 9>()
      5 list = pd.Series(data=pizzas)
      7 a_set = {2, 4, 6}
----> 9 list(a_set)

TypeError: 'Series' object is not callable

The error occurs because when we assigned the Series object to the variable name, list we overrode the built-in list() method. Then when we try to convert the set to a list, we are instead trying to call the Series object, which is not callable.

Solution

We can solve this error by using variable names not reserved for built-in functions. We can find the names of built-in functions using:

print(dir(__builtins__))

Let’s look at the revised code:

import pandas as pd

pizzas = {'margherita': 10.99 , 'pepperoni': 11.99, 'marinara': 7.99}

ser = pd.Series(data=pizzas)

a_set = {2, 4, 6}

list(a_set)

Note that we will have to create a new session if we are using an interactive Python shell so that the list variable is correctly assigned to the list() method.

Let’s run the code to get the result:

[2, 4, 6]

Summary

Congratulations on reading to the end of this tutorial!

For further reading on not callable TypeErrors, go to the articles:

To learn more about Python, specifically for data science and machine learning, go to the online courses page on Python.

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