Predictive Hacks

How to Generate a Date Series in Python

Assume that you want to generate a date series in Python having as input the start and the end date. Let’s see how we can achieve that.

import datetime

# define the start and the end date
sdate = '2023-09-10'
edate = '2023-09-14'

# define the date format
date_format = '%Y-%m-%d'

# convert the string objects to datetime and then to dates
sdate = datetime.datetime.strptime(sdate, date_format).date()
edate = datetime.datetime.strptime(edate, date_format).date()

# using list comprehension generate the list of dates
date_lst = [str(sdate+datetime.timedelta(days=x)) for x in range((edate-sdate).days)]

print(date_lst)

Output:

['2023-09-10', '2023-09-11', '2023-09-12', '2023-09-13']

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Python

Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.

Python

Intro to Chatbots with HuggingFace

In this tutorial, we will show you how to use the Transformers library from HuggingFace to build chatbot pipelines. Let’s