Open In App

Python – Iterating through a range of dates

Last Updated : 14 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to iterate DateTime through a range of dates.

Using loop and timedelta to Iterate through a range of dates

Timedelta is used to get the dates and loop is to iterate the date from the start date to end date

Syntax: delta = datetime.timedelta(days=1)

Example: Python code to display the dates from 2021 – Feb 1st to 2021 – March 1st

Python3




# import datetime module
import datetime
 
# consider the start date as 2021-february 1 st
start_date = datetime.date(2021, 2, 1)
 
# consider the end date as 2021-march 1 st
end_date = datetime.date(2021, 3, 1)
 
# delta time
delta = datetime.timedelta(days=1)
 
# iterate over range of dates
while (start_date <= end_date):
    print(start_date, end="\n")
    start_date += delta


Output:

2021-02-01
2021-02-02
2021-02-03
2021-02-04
2021-02-05
2021-02-06
2021-02-07
2021-02-08
2021-02-09
2021-02-10
2021-02-11
2021-02-12
2021-02-13
2021-02-14
2021-02-15
2021-02-16
2021-02-17
2021-02-18
2021-02-19
2021-02-20
2021-02-21
2021-02-22
2021-02-23
2021-02-24
2021-02-25
2021-02-26
2021-02-27
2021-02-28
2021-03-01

Using the dateutil library to Iterate through a range of dates

Here we are using the dateutil built-in library of Python to iterate through the given range of dates.

Syntax: rrule( frequency )

Where frequency can be DAILY/MONTHLY/ANNUALLY.

Example:

Python3




# importing the required lib
from datetime import date
from dateutil.rrule import rrule, DAILY
 
# initializing the start and end date
start_date = date(2022, 9, 1)
end_date = date(2022, 9, 11)
 
# iterating over the dates
for d in rrule(DAILY, dtstart=start_date, until=end_date):
    print(d.strftime("%Y-%m-%d"))


Output:

2022-09-01
2022-09-02
2022-09-03
2022-09-04
2022-09-05
2022-09-06
2022-09-07
2022-09-08
2022-09-09
2022-09-10
2022-09-11

Using pandas to Iterate through a range of dates

We can use the date_range() function method that is available in pandas. It is used to return a fixed frequency DatetimeIndex.

Syntax: pandas.date_range(start, end)

Parameter:

  • start is the starting date
  • end is the ending date

We can iterate to get the date using date() function.

Example:

Python3




# import pandas module
import pandas as pd
 
# specify the start date is 2021 jan 1 st
# specify the end date is 2021 feb 1 st
a = pd.date_range(start='1/1/2021', end='2/1/2021')
 
# display only date using date() function
for i in a:
    print(i.date())


Output:

2021-01-01
2021-01-02
2021-01-03
2021-01-04
2021-01-05
2021-01-06
2021-01-07
2021-01-08
2021-01-09
2021-01-10
2021-01-11
2021-01-12
2021-01-13
2021-01-14
2021-01-15
2021-01-16
2021-01-17
2021-01-18
2021-01-19
2021-01-20
2021-01-21
2021-01-22
2021-01-23
2021-01-24
2021-01-25
2021-01-26
2021-01-27
2021-01-28
2021-01-29
2021-01-30
2021-01-31
2021-02-01


Previous Article
Next Article

Similar Reads

Python - Find consecutive dates in a list of dates
Given a list of dates, the task is to write a Python program to check if all the dates are consecutive in the list. Input : [datetime(2019, 12, 30), datetime(2019, 12, 31), datetime(2020, 1, 1), datetime(2020, 1, 2), datetime(2020, 1, 3), datetime(2020, 1, 4)] Output : True Explanation : All dates are consecutive, from 30 Dec 2019 to 4 January 2020
4 min read
Python - Generate k random dates between two other dates
Given two dates, the task is to write a Python program to get K dates randomly. Input : test_date1, test_date2 = date(2015, 6, 3), date(2015, 7, 1), K = 7 Output : [datetime.date(2015, 6, 18), datetime.date(2015, 6, 25), datetime.date(2015, 6, 29), datetime.date(2015, 6, 11), datetime.date(2015, 6, 11), datetime.date(2015, 6, 10), datetime.date(201
4 min read
Python: Iterating With Python Lambda
In Python, the lambda function is an anonymous function. This one expression is evaluated and returned. Thus, We can use lambda functions as a function object. In this article, we will learn how to iterate with lambda in python. Syntax: lambda variable : expression Where, variable is used in the expressionexpression can be an mathematical expressio
2 min read
Python | Delete items from dictionary while iterating
A dictionary in Python is an ordered collection of data values. Unlike other Data Types that hold only a single value as an element, a dictionary holds the key: value pairs. Dictionary keys must be unique and must be of an immutable data type such as a: string, integer or tuple. Note: In Python 2 dictionary keys were unordered. As of Python 3, they
3 min read
Python | Iterating two lists at once
Sometimes, while working with Python list, we can have a problem in which we have to iterate over two list elements. Iterating one after another is an option, but it's more cumbersome and a one-two liner is always recommended over that. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop + "+" operator The combina
5 min read
Why is iterating over a dictionary slow in Python?
In this article, we are going to discuss why is iterating over a dict so slow in Python? Before coming to any conclusion lets have a look at the performance difference between NumPy arrays and dictionaries in python: Python Code # import modules import numpy as np import sys # compute numpy performance def np_performance(): array = np.empty(1000000
3 min read
Iterating List of Python Dictionaries
Iteration of the list of dictionaries is a very common practice that every developer performs while encountering with dictionary data. In this article, we will explore how to iterate through a list of dictionaries. Iterating List Of Dictionaries in PythonBelow are some of the ways by which we can iterate list of dictionaries in Python: Using a simp
3 min read
MoviePy – Iterating frames of Video File Clip
In this article we will see how we can iterate frames of the video file clip in MoviePy. MoviePy is a Python module for video editing, which can be used for basic operations on videos and GIF’s. A video is basically combination of lots of frame for each time there is a specific frame, in order to get the frame at given time we use get_frame method.
2 min read
Iterating over rows and columns in Pandas DataFrame
Iteration is a general term for taking each item of something, one after another. Pandas DataFrame consists of rows and columns so, to iterate over dataframe, we have to iterate a dataframe like a dictionary. In a dictionary, we iterate over the keys of the object in the same way we have to iterate in dataframe. In this article, we are using "nba.c
6 min read
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), dateti
4 min read
Practice Tags :