59

I've just encountered this issue, and couldn't find a reasonable answer for it on the front page of Google. It's similar to this question asked in 2011, but for a newer version of Python, which results in a different error message.

What is causing these TypeErrors?

Integers

import datetime
my_date = datetime.datetime.date(2021, 3, 2) 

Results in the error:

TypeError: descriptor 'date' for 'datetime.datetime' objects doesn't apply to a 'int' object

Strings

Similarly, replacing the integers with strings also gives the same error:

import datetime
my_date = datetime.datetime.date("2021", "3", "2") 

Gives:

TypeError: descriptor 'date' for 'datetime.datetime' objects doesn't apply to a 'str' object

Lists

And using a list gives the same error:

import datetime
my_date = datetime.datetime.date([2021, 3, 2]) 

Results in:

TypeError: descriptor 'date' for 'datetime.datetime' objects doesn't apply to a 'list' object

Similarly, using from datetime import datetime and datetime.date will result in the following error messages respectively:

TypeError: descriptor 'date' for 'datetime' objects doesn't apply to a 'int' object
TypeError: descriptor 'date' for 'datetime' objects doesn't apply to a 'str' object
TypeError: descriptor 'date' for 'datetime' objects doesn't apply to a 'list' object
8
  • 3
    since the datetime.datetime.date() method actually doesn't take any arguments at all, the error message seems confusing indeed Commented Mar 2, 2021 at 7:59
  • @MrFruppes Yeah, that's what threw me. I don't program in Python so often, so that error message pop up didn't really indicate to me that I was trying to call a method on a class which hadn't been instantiated. Commented Mar 2, 2021 at 19:36
  • 1
    "It's similar to this question asked in 2011, but for a newer version of Python, which results in a different error message." That doesn't make it a different problem, and therefore doesn't make it a different question. The answers are the same: the code tries to call an instance method from the class, such that self is wrong. This version is probably better, though, because it's good to keep up to date. Commented Jan 20, 2023 at 11:26
  • 1
    That said, the same problem can be caused by any instance method of any class, and a quick search implies that it is asked a fair bit, with no clear canonical. (This would be it, since it's the most popular by far of the questions showing the exact new error message; but it isn't general enough.) Commented Jan 20, 2023 at 11:27
  • @KarlKnechtel You're correct that the general case pops up a fair bit, but I think this specific case is one people hit fairly often. 2 years ago I dropped my error message into Google and nothing came up, so I wrote this Q+A. Commented Jan 22, 2023 at 20:34

2 Answers 2

106

Solution:

import datetime
my_date = datetime.date(2021, 3, 2)

or

from datetime import date
my_date = date(2021, 3, 2)

Why?

The issue is that datetime.datetime.date() is a method on a datetime.datetime object. We were confusing the datetime module with the datetime.datetime class.

What we're really looking for is the datetime.date() constructor.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! For this reason it's important to always include import statements in code examples (here and in documentations).
-3

I stumbled upon this solution which did not produce expected o.p. (0) in my dev env: (OS:MacOSX, .ipnyb, venv,python3.x) for comparing the Timestamp pandas index. For someone using Mac M1s, try below if accepted solution does not work.

import datetime
ts1 = datetime.date(2021, 3, 2)
ts2 = datetime.date(2021,3,2)

# delta
ts1 - ts2 #op =0

10 Comments

How is this different from the answer posted 3 years ago?
Below code does not work on Mac M1s as it was not explicitly mentioned. from datetime import date my_date = date(2021, 3, 2)
It works for me in my MacBook M1.
There's no difference between import datetime followed by datetime.date and from datetime import date followed by date. This is a general python feature of importing.
The accepted answer has both syntaxes.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.