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

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -