I am trying to import datetime
module, which I use to create functions to be applied over a pandas
dataframe column:
Here's the code so far:
### importing modules ###
import time
from datetime import datetime, date, time, timedelta
import dateutil.relativedelta
### function to return last day of month date ###
def last_day_of_month(date):
if date.month == 12:
return date.replace(day=31)
return date.replace(month=date.month+1, day=1) - datetime.timedelta(days=1)
### function to return a date difference ###
def set_data_loa(data_de_retorno):
if pd.isnull(data_de_retorno):
d1 = datetime.now()
d2 = d1 - dateutil.relativedelta.relativedelta(months=1)
return last_day_of_month(d2)
else:
return data_de_retorno
###################### applying over datetime64 type pandas df column ##############
input_1['data_para_loa'] = [set_data_loa(data_de_retorno) for data_de_retorno in input_1['data_de_retorno']]
The next error arises
AttributeError: type object 'datetime.datetime' has no attribute 'timedelta'
I am running this code in a Jupyter Notebook IDE,
What am I missing? How could I properly import datetime
module?
When you do from some_package import some_function
you no longer need to prepend the function with the package name using dot syntax, eg:
from random import choice
choice("ABCD")
vs
import random
random.choice("ABCD")
You've imported timedelta
from datetime
. So you need to reference it as timedelta
, not as datetime.timedelta
. Just like you can't reference date
as datetime.date
in this example because you imported date
from datetime
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.