When working with Python’s datetime module, you might encounter the error:
TypeError: descriptor 'date' for 'datetime.datetime' objects doesn't apply to a 'int' object
This error typically occurs when you try to call the date method of datetime.datetime incorrectly. Let’s break down the cause of the error and how to resolve it.
Table of contents
Example: descriptor ‘date’ for ‘datetime.datetime’ objects doesn’t apply to a ‘int’ object
You might come across this issue when attempting to instantiate a date object incorrectly, as in the following example:
import datetime my_date = datetime.datetime.date(2021, 3, 2)
You might come across this issue when attempting to instantiate a date object incorrectly, as in the following example:
TypeError: descriptor 'date' for 'datetime.datetime' objects doesn't apply to a 'int' object
Similarly, using strings or lists instead of integers will yield the same error:
Example: descriptor ‘date’ for ‘datetime.datetime’ objects doesn’t apply to a ‘str’ object
Here is an example with strings:
import datetime
my_date = datetime.datetime.date("2021", "3", "2")
Which results in:
TypeError: descriptor 'date' for 'datetime.datetime' objects doesn't apply to a 'str' object
Example: descriptor ‘date’ for ‘datetime.datetime’ objects doesn’t apply to a ‘list’ object
Here is an example with a list:
import datetime my_date = datetime.datetime.date([2021, 3, 2])
Which results in:
TypeError: descriptor 'date' for 'datetime.datetime' objects doesn't apply to a 'list' object
What Causes the Error?
In Python, datetime.datetime.date() is not a standalone method to create a date. It is a descriptor that extracts the date part from a datetime object, not a method to initialize a new date. The correct way to use the date method is on an already instantiated datetime object. When you attempt to pass integers, strings, or lists directly to datetime.datetime.date(), Python doesn’t know how to apply the descriptor to these data types, resulting in the TypeError.
Solution
Instead of using datetime.datetime.date() directly, you should be using the datetime.date() constructor to create date objects.
Fix 1: Use datetime.date() to Create a Date Object
import datetime my_date = datetime.date(2021, 3, 2) print(my_date) # Output: 2021-03-02
Here, datetime.date(year, month, day) correctly creates a date object using the provided integers.
Fix 2: Create a datetime Object First, Then Extract the Date
If you want to create a datetime object and then extract the date part, you can do it as follows:
import datetime dt = datetime.datetime(2021, 3, 2, 15, 30) # Creates a datetime object with time my_date = dt.date() # Extracts the date part print(my_date) # Output: 2021-03-02
In this example, we create a datetime object with both date and time components. By calling dt.date(), we extract just the date portion of the datetime object.
Common Mistakes and How to Avoid Them
- Passing Strings or Lists to
datetime.datetime.date: As demonstrated, passing any non-datetimeobject (like integers, strings, or lists) directly to thedatemethod will result in aTypeError. Usedatetime.date()instead for creating adateobject directly. - Confusion between
datetime.date()anddatetime.datetime.date():datetime.date()is the constructor to createdateobjects, whiledatetime.datetime.date()is the method used to extract the date part from adatetimeobject. Ensure you’re using the right function for your use case.
Conclusion
The TypeError: descriptor 'date' for 'datetime.datetime' objects doesn't apply to object occurs because datetime.datetime.date is a method meant to extract the date portion from a datetime object, not a constructor for creating a date object. To fix this error, you should use the datetime.date() constructor to create dates directly. If you’re working with a datetime object, use the .date() method to extract the date.
Congratulations on reading to the end of this tutorial!
For further reading on datetime TypeErrors, go to:
How to Solve Python TypeError: ‘datetime.datetime’ object is not callable
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.
