Skip to Content

Pandas – How to Create a date range?

In this tutorial, we’ll try to have a look at different methods used to create a date range in pandas with different functionalities.

How to Create a date range?

We can create date ranges with function pandas.date_range() which is a built-in function in pandas

Basic Syntax:

import pandas as pd
pd.date_range(start, end, periods, freq, …)

start: The start date
end: The end date
periods: The number of periods to generate
freq: The frequency to use (Refer to this link to know more about it)

Using this inbuilt function we can generate the data ranges in 3 different patterns:

  1. Data range with individual dates
  2. Date Range with Specific Number of Periods
  3. Date Range with Specific Frequency

Method -1: Data range with individual dates

create a date range in pandas

In this method, we give the start date and end date and generate all the individual dates between them.
Code:

pd.date_range(start='5/5/1995', end='5/15/1995')

This gives the date ranges containing all the 10 dates between the start date and end date

Method -2: Date Range with Specific Number of Periods

In this method, we give the start date and the end date along with the period and generate dates that are of equally spaced periods from the start date to the end date.
Code:

pd.date_range(start='5/5/1995', end='5/15/1995', periods=3)

This gives the date ranges containing 3 equally spaced days in the range from the start to the end date.

Method -3: Date Range with Specific Frequency

In this method, the below code shows how to create a date range that starts on a specific date and has a month-level frequency for six periods from the start date:

Code:

pd.date_range(start='1/1/2020', freq='MS', periods=6)

The above code gives a series of six dates that are each one month apart. Note that ‘MS‘ stands for ‘Month Start.’ You can find a complete list of date aliases here.

Examples

Now, let us try to have a look at examples of each method discussed above

Method – 1: Example

Code

import pandas as pd
pd.date_range(start='5/5/1995', end='5/15/1995')

Output:

DatetimeIndex(['1995-05-05', '1995-05-06', '1995-05-07', '1995-05-08',
               '1995-05-09', '1995-05-10', '1995-05-11', '1995-05-12',
               '1995-05-13', '1995-05-14', '1995-05-15'],
              dtype='datetime64[ns]', freq='D')

Method – 2: Example

Code:

import pandas as pd
pd.date_range(start='5/5/1995', end='5/15/1995', periods=3)

Output:

DatetimeIndex(['1995-05-05', '1995-05-10', '1995-05-15'], dtype='datetime64[ns]', freq=None)

Method – 3: Example

Code:

import pandas as pd
pd.date_range(start='1/1/2020', freq='MS', periods=6)

Output:

DatetimeIndex(['2020-01-01', '2020-02-01', '2020-03-01', '2020-04-01',
               '2020-05-01', '2020-06-01'],
              dtype='datetime64[ns]', freq='MS')

Summary

In this tutorial, we looked at different methods used to create a date range in pandas.
We understood the different ways of using the built-in function pandas.date_range() to create different date ranges.

  • Data range with individual dates.
  • Date Range with a specific number of periods.
  • Date Range with a specific frequency.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.