How to use pandas to Iterate through a range of dates In Python

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


Python – Iterating through a range of dates

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

Similar Reads

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...

Using the dateutil library to Iterate through a range of dates

...

Using pandas 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....