0

I have a Dataframe df and I am trying to find the difference, in months, between df['maturity_dt'] and todays date and use the code below to do this, but I get the error ValueError: time data '2015-12-31 00:00:00.0' does not match format '%Y-%b-%d %H:%M:%S'. I guess I don't know how to refer to the last 0 in 00:00:00.0

def months_to_maturity_func(d1, d2):
    return abs((d2 - d1).months)

todays_date = datetime.date.today()

for (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

2
  • Use %f for micro-secondes. Commented Jul 7, 2015 at 16:28
  • And also replace the %b with %m. The following works for me: datetime.datetime.strptime('2015-12-31 00:00:00.0', '%Y-%m-%d %H:%M:%S.%f') Commented Jul 7, 2015 at 16:29

1 Answer 1

2

You are using the wrong format , try using - %Y-%m-%d %H:%M:%S.%f .

%b is for three letter month abbreviations like - Jan , Feb , etc.

And use %f at the end for microseconds.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.