Troubleshooting "descriptor 'date' requires a 'datetime.datetime' object but received a 'int'"
PythonDatetimePython 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.