Pandas date_range Method example
Pandas date_range Method example

Pandas date_range Method Implementation in Python with Example

GET FREE AMZAON AUDIOBOOKS

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

Date Range from 1-DEC-2020 to 31-DEC-2020
Date Range from 1-DEC-2020 to 31-DEC-2020

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 )
Generating dates using start and periods argument
Generating dates using start and periods argument

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

Generating dates using end and periods argument
Generating dates using end and periods argument

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)
Generating dates using start, end and periods argument
Generating dates using start, end and periods argument

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")
Generating Weekly dates
Generating Weekly dates

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")
Generating Monthly dates
Generating Monthly dates
pd.date_range(start="01-01-2020",end="12-31-2020",freq="3M")
Generating Quarterly dates
Generating Quarterly dates

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:

Pandas Documentation

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner