Creating a list of dates using pd.date_range
In this method, we will use pandas date_range to create a list of ranges of dates in Python.
Python3
import datetime import pandas as pd # initializing date test_date = datetime.datetime.strptime( "01-7-2022" , "%d-%m-%Y" ) # initializing K K = 5 date_generated = pd.date_range(test_date, periods = K) print (date_generated.strftime( "%d-%m-%Y" )) |
Output:
Index(['01-07-2022', '02-07-2022', '03-07-2022', '04-07-2022', '05-07-2022'], dtype='object')
Creating a list of range of dates in Python
Given a date, the task is to write a Python program to create a list of a range of dates with the next K dates starting from the current date.
Examples:
Input : test_date = datetime.datetime(1997, 1, 4), K = 5
Output : [datetime.datetime(1997, 1, 4), datetime.datetime(1997, 1, 5), datetime.datetime(1997, 1, 6), datetime.datetime(1997, 1, 7), datetime.datetime(1997, 1, 8)]
Explanation : 5 dates after 4 January are extracted in list.