ValueError when converting String to datetime in Python -
i have dataframe df
, trying find difference, in months, between df['maturity_dt']
, todays date , use code below this, error valueerror: time data '2015-12-31 00:00:00.0' not match format '%y-%b-%d %h:%m:%s'
. guess don't know how refer last 0
in 00:00:00.0
def months_to_maturity_func(d1, d2): return abs((d2 - d1).months) todays_date = datetime.date.today() (i,row) in df.iterrows(): row['months_to_maturity'] = months_to_maturity_func(todays_date, datetime.datetime.strptime(row['maturity_dt'], '%y-%b-%d %h:%m:%s'))
thank you
you using wrong format , try using - %y-%m-%d %h:%m:%s.%f
.
%b
3 letter month abbreviations - jan
, feb
, etc.
and use %f
@ end microseconds.
Comments
Post a Comment