How to generate range of dates in pandas
In this example, we’ll create a list of pandas
Timestamp
objects that represent 100 consecutive days, starting at a fixed date:
start_date = pd.Timestamp("2020-03-01")
Generating the 100 consecutive days is easy:
all_days = [start_date + pd.Timedelta(d, "days") for d in range(100)]
Note that range(100)
will generate all numbers from 0
up to and including 99
. Hence, [pd.Timedelta(d, "days") for d in range(100)]
will generate a list of Timedelta
s that represent 0 days, 1 days, 2 days, …, 99 days.
Full example:
import pandas as pd
start_date = pd.Timestamp("2020-03-01")
all_days = [start_date + pd.Timedelta(d, "days") for d in range(100)]
print(all_days)