Pandas is a very popular python module for data manipulation. If you are working on time-series data then panda date_range is a very useful method for grouping dates according to days, weeks, or months. In this entire post, I will show the various examples to implement this pandas method.
Syntax of the Pandas date_range
pandas.date_range(start=None, end=None, periods=None, freq=None, tz=None, normalize=False,
name=None, closed=None, **kwargs)
There are many parameters in the methods. But I will explain only the most used parameters.
start: Starting date. It is the left bound for generating dates.
end: Ending date. It is the upper bound for generating dates.
periods: Number of periods to generate.
freq: It is used to generate dates on the basis of frequency like “D”, “M” e.t.c
The method returns the DateTime index.
You can know more about the other parameters on the Offical Pandas Website
Examples for Implementing Pandas date_range
Example 1: Creating a range of dates with daily frequency.
Suppose I want to get the dates from “01-12-2020” to “31-12-2020”. Then I will use the below code.
pd.date_range(start="12-01-2020",end="12-31-2020" )
Output

Note: You should also remember that format of the date should month, then the day, and last year.
Example 2: Generating dates using periods
You can also generate the next dates using the only start date. But you have to use extra argument periods. For example, I want to get the dates from “1 Jan-2020″ to ” 10-Jan-2020″ then I will execute the following code.
pd.date_range(start="01-01-2020",periods=10 )

In this same way, you can generate a date by specifying the end date only.
pd.date_range(end="01-01-2020",periods=10 )
Output

You can also use both the start and end dates with the period. It generates the dates between them with the period defined.
pd.date_range(start="12-01-2020",end="12-31-2020", periods=10)

Example 3: Generating dates using frequency parameters.
You can generate weekly dates, monthly dates, quarterly dates using the frequency parameter. For example, I want weekly dates in the range then I will execute the following code.
pd.date_range(start="12-01-2020",end="12-31-2020",freq="W")

In the same way, you can generate dates on a monthly and quarterly basis.
pd.date_range(start="01-01-2020",end="12-31-2020",freq="M")

pd.date_range(start="01-01-2020",end="12-31-2020",freq="3M")

Conclusion
These are the implementation of the pandas date_range method. This function is very useful in analyzing time-series data. I hope you have fully understood this function. Even if you have any queries then you can contact us for more information.
Source:
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.