Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a range of dates in Python

I want to create a list of dates, starting with today, and going back an arbitrary number of days, say, in my example 100 days. Is there a better way to do it than this?

import datetime  a = datetime.datetime.today() numdays = 100 dateList = [] for x in range (0, numdays):     dateList.append(a - datetime.timedelta(days = x)) print dateList 
like image 622
Thomas Browne Avatar asked Jun 14 '09 18:06

Thomas Browne


People also ask

How do I create a date range in pandas?

You can use the pandas. date_range() function to create a date range in pandas.


1 Answers

Marginally better...

base = datetime.datetime.today() date_list = [base - datetime.timedelta(days=x) for x in range(numdays)] 
like image 196
S.Lott Avatar answered Oct 03 '22 20:10

S.Lott