ValueError while converting String to datetime

I have a dataframe as follows and I am trying to shrink the dataframe to only contain rows for which Date is greater than a variable curve_enddate

. df['Date']

is in datetime

and hence I am trying to convert curve_enddate[i][0]

that gives the form string 2015-06-24

to datetime

, but I am getting an 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 is:

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

      

thank

+3


source to share


2 answers


You cannot compare the time.struct_time tuple that returns time.strptime

in Timestamp

, so you also need to change that and also use '%Y-%m-%d'

using m

which is the month of the decimal. You can use pd.to_datetime

to create an object for comparison:



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

      

+2


source


You are using the wrong date format %b

for named months (type abbreviations Jan

or Feb

etc.), use %m

for numbered months.

Code -



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

      

+4


source







All Articles