The TypeError ‘datetime.datetime’ object is not callable occurs when you try to call a datetime.datetime
object by putting parenthesis ()
after it like a function. Only functions respond to function calls.
This error commonly occurs when you override the name for a built-in class or method from the datetime module.
You can solve the error by avoiding naming variables after class or method names that you want to import.
This tutorial will go through the error in detail and how to solve it with code examples.
TypeError: ‘datetime.datetime’ 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()
.
Objects of the datetime.datetime class do not respond to function calls because they are not functions. If you try to call a datetime.datetime
object as if it were a function, you will raise the TypeError: ‘datetime.datetime’ 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 datetime.datetime
object with the callable method:
from datetime import datetime today = datetime.today() print(callable(today))
False
The callable function returns False
for the datetime.datetime
object.
Example
Let’s look at an example of attempting to call a datetime.datetime
object. First, we will import the datetime
and date
classes from the datetime
module and then create a datetime
object for today’s date.
from datetime import datetime, date date = datetime.today()
Next, we will try to create a date object and print the date to the console.
next_week = date(2022, 6, 29) print(next_week)
The error occurs because we defined a datetime.datetime
object and assigned it to the variable name date
. Then when we try to create a datetime.date
object using the date()
constructor, we are instead trying to call the datetime.datetime
object called date
, which is not callable.
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Input In [2], in <cell line: 5>() 1 from datetime import datetime, date 3 date = datetime.today() ----> 5 next_week = date(2022, 6, 29) 7 print(next_week) TypeError: 'datetime.datetime' object is not callable
Solution
We can solve this error by using variable names not reserved for class or method names which we want to use.
We can find the names of the classes and methods in the datetime
module using the dir()
method as follows:
import datetime print(dir(datetime))
['__add__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__radd__', '__reduce__', '__reduce_ex__', '__repr__', '__rsub__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', 'astimezone', 'combine', 'ctime', 'date', 'day', 'dst', 'fold', 'fromisocalendar', 'fromisoformat', 'fromordinal', 'fromtimestamp', 'hour', 'isocalendar', 'isoformat', 'isoweekday', 'max', 'microsecond', 'min', 'minute', 'month', 'now', 'replace', 'resolution', 'second', 'strftime', 'strptime', 'time', 'timestamp', 'timetuple', 'timetz', 'today', 'toordinal', 'tzinfo', 'tzname', 'utcfromtimestamp', 'utcnow', 'utcoffset', 'utctimetuple', 'weekday', 'year']
Let’s look at the revised code:
from datetime import datetime, date todays_date = datetime.today() next_week = date(2022, 6, 29) print(next_week)
We renamed the datetime.datetime
object to todays_date
allowing us to create the date
object using the date()
constructor.
Let’s run the code to get the result:
2022-06-29
Summary
Congratulations on reading to the end of this tutorial!
For further reading on not callable TypeErrors, go to the articles:
- How to Solve Python TypeError: ‘tuple’ object is not callable.
- How to Solve Python TypeError: ‘bool’ object is not callable.
- How to Solve Python TypeError: ‘DataFrame’ object is not callable.
For further reading on datetime TypeErrors, go to the article:
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!
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.