Troubleshooting "descriptor 'date' requires a 'datetime.datetime' object but received a 'int'"

PythonDatetime

Python Problem Overview


In my code I ask the user for a date in the format dd/mm/yyyy.

currentdate = raw_input("Please enter todays date in the format dd/mm/yyyy: ")
day,month,year = currentdate.split('/')
today = datetime.date(int(year),int(month),int(day))

This returns the error

> TypeError: descriptor 'date' requires a 'datetime.datetime' object but received a 'int'

if I remove the int() then I end up with the same error only it says it received a 'str'

What am I doing wrong?

Python Solutions


Solution 1 - Python

It seems that you have imported datetime.datetime module instead of datetime. This should work though:

import datetime
currentdate = raw_input("Please enter todays date in the format dd/mm/yyyy: ")
day,month,year = currentdate.split('/')
today = datetime.date(int(year),int(month),int(day))

..or this:

from datetime import date
currentdate = raw_input("Please enter todays date in the format dd/mm/yyyy: ")
day,month,year = currentdate.split('/')
today = date(int(year),int(month),int(day))

Solution 2 - Python

Do you import like this?

from datetime import datetime

Then you must change it to look like this:

import datetime

Explanation: In the first case you are effectively calling datetime.datetime.date(), a method on the object datetime in the module datetime. In the later case you create a new date() object with the constructor datetime.date().

Alternatively, you can change the import to:

from datetime import datetime, date

and then construct with date(y,m,d) (without the datetime. prefix).

Solution 3 - Python

if you already have

from datetime import datetime

then you can construct like so:

christmas = datetime(2013,12,25)

Solution 4 - Python

You can use both datetime and datetime.datetime. Write the imports like this:

from datetime import datetime
import datetime as dt

time_1 = datetime.strptime('17:00:00', '%H:%M:%S')
time_1 = dt.time(time_1.hour, time_1.minute,  time_1.second)
    

Solution 5 - Python

I can reproduce the error if I do

from datetime import *

It goes away when I do

import datetime

So check your imports.

Solution 6 - Python

I suspect that the datetime reference the object and not the module. You probably did have the following code (probably more complex):

from datetime import datetime
currentdate = raw_input("Please enter todays date in the format dd/mm/yyyy: ")
day,month,year = currentdate.split('/')
today = datetime.date(int(year),int(month),int(day))

You are thus calling the date method of the datetime class instead of calling the date function of the datetime module.

You can print the datetime object to see if this is really the case:

>>> import datetime
>>> print datetime
<module 'datetime' (built-in)>
>>> print datetime.date(1, 1, 1)
0001-01-01
>>> datetime = datetime.datetime
>>> print datetime
<type 'datetime.datetime'>
>>> print datetime.date(1, 1, 1)

Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    print datetime.date(1, 1, 1)
TypeError: descriptor 'date' requires a 'datetime.datetime' object but received a 'int'

Solution 7 - Python

> TypeError: descriptor 'date' requires a 'datetime.datetime' object but received a 'int'

This is because you have used variables like year, month, day.

Use something like this:

year1, month1, day1 =  [int(d) for d in startDate.split('-')]
print(date(year1, month1, day1))

and it will work.

Solution 8 - Python

The error suggest's your import looks fine. Instead, while doing an operation using datetime, make sure the values are converted to datetime format first.

use pandas.to_datetime to do the same, before you use any operation on the same.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohn mcnamaraView Question on Stackoverflow
Solution 1 - PythonplaesView Answer on Stackoverflow
Solution 2 - PythonBoldewynView Answer on Stackoverflow
Solution 3 - PythonTim RichardsonView Answer on Stackoverflow
Solution 4 - PythonJeanne LaneView Answer on Stackoverflow
Solution 5 - PythonFred FooView Answer on Stackoverflow
Solution 6 - PythonSylvain DefresneView Answer on Stackoverflow
Solution 7 - PythonGulshan PrajapatiView Answer on Stackoverflow
Solution 8 - PythonAbhinav BangiaView Answer on Stackoverflow