Creating a range of dates in Python
--
Track title: CC C Schuberts Piano Sonata No 13 D
--
Chapters
00:00 Question
00:30 Accepted answer (Score 681)
00:44 Answer 2 (Score 422)
01:54 Answer 3 (Score 134)
02:16 Answer 4 (Score 50)
03:23 Thank you
--
Full question
https://stackoverflow.com/questions/9933...
Answer 1 links:
[Pandas]: http://pandas.pydata.org/
[pd.date_range()]: https://pandas.pydata.org/pandas-docs/st...
[bdate_range]: https://pandas.pydata.org/pandas-docs/st...
[date range documentation]: https://pandas.pydata.org/pandas-docs/st...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #datetime #date
#avk47
ACCEPTED ANSWER
Score 721
Marginally better...
base = datetime.datetime.today()
date_list = [base - datetime.timedelta(days=x) for x in range(numdays)]
ANSWER 2
Score 455
Pandas is great for time series in general, and has direct support for date ranges.
For example pd.date_range():
import pandas as pd
from datetime import datetime
datelist = pd.date_range(datetime.today(), periods=100).tolist()
It also has lots of options to make life easier. For example if you only wanted weekdays, you would just swap in bdate_range.
In addition it fully supports pytz timezones and can smoothly span spring/autumn DST shifts.
EDIT by OP:
If you need actual python datetimes, as opposed to Pandas timestamps:
import pandas as pd
from datetime import datetime
pd.date_range(end = datetime.today(), periods = 100).to_pydatetime().tolist()
#OR
pd.date_range(start="2018-09-09",end="2020-02-02")
This uses the "end" parameter to match the original question, but if you want descending dates:
pd.date_range(datetime.today(), periods=100).to_pydatetime().tolist()
ANSWER 3
Score 52
You can write a generator function that returns date objects starting from today:
import datetime
def date_generator():
from_date = datetime.datetime.today()
while True:
yield from_date
from_date = from_date - datetime.timedelta(days=1)
This generator returns dates starting from today and going backwards one day at a time. Here is how to take the first 3 dates:
>>> import itertools
>>> dates = itertools.islice(date_generator(), 3)
>>> list(dates)
[datetime.datetime(2009, 6, 14, 19, 12, 21, 703890), datetime.datetime(2009, 6, 13, 19, 12, 21, 703890), datetime.datetime(2009, 6, 12, 19, 12, 21, 703890)]
The advantage of this approach over a loop or list comprehension is that you can go back as many times as you want.
Edit
A more compact version using a generator expression instead of a function:
date_generator = (datetime.datetime.today() - datetime.timedelta(days=i) for i in itertools.count())
Usage:
>>> dates = itertools.islice(date_generator, 3)
>>> list(dates)
[datetime.datetime(2009, 6, 15, 1, 32, 37, 286765), datetime.datetime(2009, 6, 14, 1, 32, 37, 286836), datetime.datetime(2009, 6, 13, 1, 32, 37, 286859)]
ANSWER 4
Score 45
If using an external dependency is OK, check https://pypi.org/project/python-dateutil and its rrule module: https://dateutil.readthedocs.io/en/stable/rrule.html.
from dateutil import rrule
from datetime import datetime
list(rrule.rrule(rrule.DAILY,count=100,dtstart=datetime.now()))