datetime - extracting days between a dataframe date and today -
i have date in dataframe , create dataframe column has number of days between date (option expiration) , today. date looks this:
df2.expiration.head(1) 0 05/29/15 name: expiration, dtype: object
i tried this:
from datetime import datetime def compare_dates(date): date_format = '%m/%d/%yy' current_date = datetime.strptime(date, date_format) today = datetime.today() diff = current_date - today return diff.days df2['days_since'] = df2['expiration'].apply(compare_dates)
but long error message final line of:
valueerror: time data '05/29/15' not match format 'mm/dd/yy'
thank once again ! john
try following (note lower case 'y'):
date_format = '%m/%d/%y'
%y
4 digit year, , %yy
4 digit year ending in 'y', e.g. '2015y'.
s = '05/29/15' >>> dt.datetime.strptime(s, '%m/%d/%y').date() datetime.date(2015, 5, 29)
Comments
Post a Comment