datetime - Python find index of all dates between two dates -
i trying implement equivalent of numpy.where
dates follows:
from datetime import date, timedelta td, datetime d1 = datetime.strptime('1/1/1995', "%m/%d/%y") d2 = datetime.strptime('12/31/2015', "%m/%d/%y") alldays = [] while(d1<=d2): alldays.append(d1) d1 = d1 + td(days=1) validdate = alldays trainstdt = '1/1/1995' trainendt = '12/31/2013' teststdt = '1/1/2014' testendt = '12/31/2015' indtrain = (validdate >= datetime.strptime(trainstdt,'%m/%d/%y')) & (validdate <= datetime.strptime(trainendt,'%m/%d/%y')) indtest = (validdate >= datetime.strptime(teststdt,'%m/%d/%y')) & (validdate <= datetime.strptime(testendt,'%m/%d/%y')) traindates = validdate[indtrain] testdates = validdate[indtest] print traindates[0] print traindates[-1:] print testdates[0] print testdates[-1:]
however: (1) indtrain doesn't work trying compare list datetime (2) solution loop through each element of validdates
is there better way it?
just turn list array. add import numpy np
top of script, , after while
loop, add:
alldays = np.array(alldays)
Comments
Post a Comment