user131983
user131983

Reputation: 3927

ValueError when converting String to datetime

I have a dataframe as follows, and I am trying to reduce the dataframe to only contain rows for which the Date is greater than a variable curve_enddate. The df['Date'] is in datetime and hence I'm trying to convert curve_enddate[i][0] which gives a string of the form 2015-06-24 to datetime but am getting the error ValueError: time data '2015-06-24' does not match format '%Y-%b-%d'.

              Date      Maturity      Yield_pct Currency
0       2015-06-24          0.25             na      CAD
1       2015-06-25          0.25   0.0948511020      CAD

The line where I get the Error:

df = df[df['Date'] > time.strptime(curve_enddate[i][0], '%Y-%b-%d')]

Thank You

Upvotes: 3

Views: 89

Answers (2)

Padraic Cunningham
Padraic Cunningham

Reputation: 180391

You cannot compare a time.struct_time tuple which is what time.strptime returns to a Timestamp so you also need to change that as well as using '%Y-%m-%d' using m which is the month as a decimal number. You can use pd.to_datetime to create the object to compare:

df = df[df['Date'] > pd.to_datetime(curve_enddate[i][0], '%Y-%m-%d')]

Upvotes: 2

Anand S Kumar
Anand S Kumar

Reputation: 90889

You are using wrong date format, %b is for the named months (abbreviations like Jan or Feb , etc), use %m for the numbered months.

Code -

df = df[df['Date'] > time.strptime(curve_enddate[i][0], '%Y-%m-%d')]

Upvotes: 4

Related Questions