AttributeError: module 'datetime' has no attribute 'today' | datetime - python

Keep getting this error that the package is not being installed. Any Fixes?
from datetime import datetime, date, timedelta
time_change = datetime.timedelta(hours=2)
today_var = datetime.today().date() + time_change
print(today_var)
AttributeError: module 'datetime' has no attribute 'today'

Try this:
time_change = timedelta(hours=2)
You already imported datetime.timedelta as timedelta so you can use that.
But you imported datetime.datetime as datetime so that is why it is saying 'datetime.datetime' has no attribute 'timedelta'.

Related

AttributeError: 'DataFrame' object has no attribute 'allah1__27'

I'm trying to solve this and I'm pretty sure the code is right but it keeps getting me the same Error.
I have tried this:
import datetime
from datetime import datetime as datet
test_df = shapefile.copy()
test_df['timestamp'] = prediction_time
test_df['allah1__27'] = shapefile.allah1__27.astype('int64')
test_df['hour'] = prediction_time.hour
test_df['weekday'] = prediction_time.weekday()
test_df['month'] = prediction_time.month
def add_join_key(df):
df['join_key'] = df.allah1__27.map(int).map(str)+df.timestamp.map(datetime.isoformat)
df = df.set_index('join_key')
return df
weath_df = wdf.loc[prediction_time]
test_df = add_join_key(test_df)
weath_df = add_join_key(weath_df.reset_index())
And I get this Error:
AttributeError: module 'datetime' has no attribute 'isoformat'
Also I tried:
def add_join_key(df):
df['join_key'] = df.allah1__27.map(int).map(str)+df.timestamp.map(datet.isoformat)
df = df.set_index('join_key')
return df
weath_df = wdf.loc[prediction_time]
test_df = add_join_key(test_df)
weath_df = add_join_key(weath_df.reset_index())
And I get this Error:
AttributeError: DataFrame' object has no attribute 'allah1__27'
Did I miss something?
For the first error: the method isoformat is from datetime that's a method of datetime.
You should:
import datetime
datetime.datetime.isoformat
Or:
from datetime import datetime as datet
datet.isoformat
As for the second error:
df is a dictionary, i think you should call it:
df['join_key'] = df['allah1__27'].map(int).....

How to add tzinfo to date?

I have the following vars:
time_created = datetime.utcnow() and time_created_day = datetime.utcnow().date().
I cannot save time_created_day to the db because of AttributeError: 'datetime.date' object has no attribute 'tzinfo'
How to fix this (add tzinfo)?
Ok, here is one way that I did this.
If you need to add tzinfo, you may use the below.
from datetime import datetime
import pytz
time_created_day = datetime.datetime.utcnow().date()
time_created_day_with_tz_info = datetime(time_created_day.year,
time_created_day.month, time_created_day.day, tzinfo=pytz.timezone('UTC'))
> print time_created_day_with_tz_info
> datetime.datetime(2018, 5, 9, 0, 0, tzinfo=<UTC>)

Date input format? [duplicate]

This question already has answers here:
AttributeError: 'datetime' module has no attribute 'strptime'
(5 answers)
Closed 5 years ago.
I have code like this:
import time
import datetime
def dates():
date1 = str(input('Date start: '))
try:
dt_start = datetime.strptime(date1, '%d, %m, %Y')
except ValueError:
print ("Incorrect format")
date2 = str(input('Date end: '))
try:
dt_end = datetime.strptime(date2, '%d, %m, %Y')
except ValueError:
print ("Incorrect format")
if date1 > date2:
print("Error!")
dates()
And I want to define date input format like d.m.Y.
For example, when I input "17.12.1995". I got error:
'module' object has no attribute 'strptime'.
How to define user input format?
datetime is a module, there is a class with the same name (datetime) within it and that class has a class method strptime(). You need to either call it like:
dt_start = datetime.datetime.strptime(date1, '%d, %m, %Y')
Or to change your import statement:
from datetime import datetime
so that it imports only the datetime class to your current namespace.

Why am I getting the error: 'method_descriptor' object has no attribute 'now'?

If I run the method
from datetime import datetime
from timedelta import time
def action_retbook(self, cr, uid, ids, context=None):
dt = datetime.date.now()
todaydate = datetime.strptime(dt, "%y/%m/%d")
x=6
cr.execute("""update rmkbook_issue set dt_of_return ='%s' where id= %s """ %(todaydate, x))
return
I get the error 'method_descriptor' object has no attribute 'now'. Why?
You can run the method now() like this:
dt = datetime.now()
You can check this question as well
I believe
from datetime import datetime
datetime.now()
Works.. in python 3.9.7

python debug straightforward datetime object

I cannot understand why this is failing, can someone please explain:
import datetime
def test(timestamp):
xt = datetime(2013, 4,4)
dt = datetime.strptime(timestamp, '%d/%m/%Y %H:%M:%S')
print (xt,dt)
test('04/04/2013 08:37:20')
The error is:
Traceback (most recent call last):
File "", line 12, in
File "", line 5, in test
TypeError: 'module' object is not callable
It seems to work ok with from datetime import datetime instead. I can't understand what the difference is.
Thanks.
Because in the datetime module, there is a datetime() function, but you didn't call it ( you instead tried to call the module, that's why you got a TypeError: 'module' object is not callable).
import datetime
def test(timestamp):
xt = datetime(2013, 4,4) # Problem is this line.
dt = datetime.strptime(timestamp, '%d/%m/%Y %H:%M:%S')
print (xt,dt)
test('04/04/2013 08:37:20')
To fix the line, change it to:
xt = datetime.datetime(2013,4,4)
The reason from datetime import datetime worked was because you imported the specific function, and so you wouldn't need to do datetime.datetime().
datetime is both a module AND a class in that module. In your example you need datetime.datetime.
The
xt = datetime(2013, 4,4)
should be
xt = datetime.datetime(2013, 4,4)
Here, datetime is the name of the module. The full name of the class is datetime.datetime.
datetime is the datetime module, datetime.datetime is the datetime type:
import datetime
def test(timestamp):
xt = datetime.datetime(2013, 4,4)
dt = datetime.datetime.strptime(timestamp, '%d/%m/%Y %H:%M:%S')
print (xt,dt)
Here datetime can refer to the module or the class within the module so you have to disambiguate
import datetime
def test(timestamp):
xt = datetime.datetime(2013, 4,4)
dt = datetime.datetime.strptime(timestamp, '%d/%m/%Y %H:%M:%S')
print (xt,dt)
test('04/04/2013 08:37:20')
gives the following output:
(datetime.datetime(2013, 4, 4, 0, 0), datetime.datetime(2013, 4, 4, 8, 37, 20))

Categories

Resources