How to Fix: TypeError: ‘DataFrame’ object is not callable


One common error you may encounter when using pandas is:

TypeError: 'DataFrame' object is not callable

This error usually occurs when you attempt to perform some calculation on a variable in a pandas DataFrame by using round () brackets instead of square [ ] brackets.

The following example shows how to use this syntax in practice.

How to Reproduce the Error

Suppose we have the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
                   'points': [18, 22, 19, 14, 14, 11, 20, 28],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#view DataFrame
print(df)

  team  points  assists  rebounds
0    A      18        5        11
1    B      22        7         8
2    C      19        7        10
3    D      14        9         6
4    E      14       12         6
5    F      11        9         5
6    G      20        9         9
7    H      28        4        12

Now suppose we attempt to calculate the mean value in the “points” column:

#attempt to calculate mean value in points column
df('points').mean()

TypeError: 'DataFrame' object is not callable

Since we used round () brackets, pandas thinks that we’re attempting to call the DataFrame as a function.

Since the DataFrame is not a function, we receive an error.

How to Fix the Error

The way to resolve this error is to simply use square [ ] brackets when accessing the points column instead round () brackets:

#calculate mean value in points column
df['points'].mean()

18.25

We’re able to calculate the mean of the points column (18.25) without receiving any error since we used squared brackets.

Also note that we could use the following dot notation to calculate the mean of the points column as well:

#calculate mean value in points column
df.points.mean()

18.25

Notice that we don’t receive any error this time either.

Additional Resources

The following tutorials explain how to fix other common errors in Python:

How to Fix in Python: ‘numpy.ndarray’ object is not callable
How to Fix: TypeError: ‘numpy.float64’ object is not callable
How to Fix: Typeerror: expected string or bytes-like object

Leave a Reply

Your email address will not be published.