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

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


TypeError: ‘DataFrame’ 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().

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

We can verify is callable by using the built-in callable() method and passing the object to it. If the method returns True, then the object is callable, otherwise, if it returns False the object is not callable. Let’s look at testing the method with a DataFrame:

import pandas as pd

df = pd.DataFrame({'values':[2, 4, 6, 8, 10, 12]})

print(callable(df))
False

The callable function returns false for a DataFrame, verifying that DataFrame objects are not callable.

Example

Let’s look at an example where we want to calculate the mean monthly amount of vegetables in kilograms sold by a farmer over the course of a year. First, we will look at the dataset.

Month,Amount
1,200
2,150
3,300
4,350
5,234
6,500
7,900
8,1000
9,959
10,888
11,3000
12,1500

The first column of the CSV is the month as a number and the second column is the number of vegetables sold in that month in kilograms. We will save the dataset as veg_sold.csv.

Next, we will load the dataset into a DataFrame using pandas.

import pandas as pd

df = pd.read_csv('veg_sold.csv')

print(df)
    Month  Amount
0       1     200
1       2     150
2       3     300
3       4     350
4       5     234
5       6     500
6       7     900
7       8    1000
8       9     959
9      10     888
10     11    3000
11     12    1500

Next, we will try to calculate the mean amount sold by indexing the column name ‘Amount‘ in the DataFrame and calling mean() on the column.

mean_sold = df('Amount').mean()

print(f'Mean sold over the year: {mean_sold}kg')

Let’s run the code to see what happens:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-5237331dba60> in <module>
----> 1 mean_sold = df('Amount').mean()
      2 print(f'Mean sold over the year: {mean_sold}kg')

TypeError: 'DataFrame' object is not callable

The error occurs because we tried to access the Amount column of the DataFrame using parentheses. Putting parentheses after the DataFrame object is seen as a function call.

Solution

To solve this error, we can access the column of a DataFrame using square brackets. The resulting object will be a Series, which we can call the mean() method on. Let’s look at the revised code:

mean_sold = df['Amount'].mean()

print(f'Mean sold over the year: {mean_sold}kg')

Let’s run the code to get the result:

Mean sold over the year: 831.75kg

We can also call the mean method directly on the DataFrame. The resultant object will be a Series containing the mean of both columns. We can then access the mean of the ‘Amount‘ column using square brackets. Let’s look at the revised code:

mean_cols = df.mean()
print(f'Mean sold over the year: {mean_cols["Amount"]}kg')
Mean sold over the year: 831.75kg

Summary

Congratulations on reading to the end of this tutorial. The TypeError ‘DataFrame’ object is not callable occurs when you try to call a DataFrame as if it were a function. TypeErrors occur when you attempt to perform an illegal operation for a specific data type.

To solve this error, ensure that there are no parentheses after the DataFrames in your code. You can check if an object is a DataFrame by using the built-in type() method.

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!