How to generate a list of dates in Python
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
The easiest method I've seen so far is using the Pandas library.
The method is called pandas.date_range()
Using frequency
# we import the library
import pandas as pd
# We generate the list of dates
list_dates = pd.date_range(start="2021-01-01", end="2021-06-01", freq="D")
We pass the start date and the end date as arguments. As you can see we also pass the frequency, here it will be every day from start date until end date.
You can also pass various frequencies to the freq parameter.
The list is available here.
e.g. One of the most useful is month start.
Or even a 10 days frequency.
Using periods
Instead of passing the frequency, you can also pass the number of periods you want in between those two dates.
# We generate the list of dates
list_dates = pd.date_range(start="2021-01-01", end="2021-06-01", periods=30)
Here you are !
You know now how to generate a list of dates in Python using Pandas.
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
Related Articles
Continue your learning journey with these related topics
Master Data Science in Days, Not Months 🚀
Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚