How to generate a list of dates in Python

1 min

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.

# We generate the list of dates with a month start frequency
list_dates = pd.date_range(start="2021-01-01", end="2021-06-01", freq="MS")
Using month start as frequency

Or even a 10 days frequency.

# We generate the list of dates with a 10 days frequency
list_dates = pd.date_range(start="2021-01-01", end="2021-06-01", freq="10D")
Using 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.