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:
generate_dates.py
start_date = pd.Timestamp("2020-03-01")Generating the 100 consecutive days is easy:
generate_dates.py
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 Timedeltas that represent 0 days, 1 days, 2 days, …, 99 days.
Full example:
generate_dates_full.py
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)If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow