Home » How to Create a Date Range in Pandas (3 Examples)

How to Create a Date Range in Pandas (3 Examples)

by Tutor Aspire

You can use the pandas.date_range() function to create a date range in pandas.

This function uses the following basic syntax:

pandas.date_range(start, end, periods, freq, …)

where:

  • start: The start date
  • end: The end date
  • periods: The number of periods to generate
  • freq: The frequency to use (refer to this list for frequency aliases)

The following examples show how to use this function in practice.

Example 1: Create Date Range with Individual Days

The following code shows how to create a date range composed of individual days with a specific start and end date:

import pandas as pd

#create 10-day date range 
pd.date_range(start='1/1/2020', end='1/10/2020')

DatetimeIndex(['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-04',
               '2020-01-05', '2020-01-06', '2020-01-07', '2020-01-08',
               '2020-01-09', '2020-01-10'],
              dtype='datetime64[ns]', freq='D')

The result is a list of 10 days that range from the specified start date to the specified end date.

Example 2: Create Date Range with Specific Number of Periods

The following code shows how to create a date range that has a specific number of equally-spaced periods between a certain start and end date:

import pandas as pd

#create 10-day date range with 3 equally-spaced periods
pd.date_range(start='1/1/2020', end='1/10/2020', periods=3)

DatetimeIndex(['2020-01-01 00:00:00', '2020-01-05 12:00:00',
               '2020-01-10 00:00:00'],
              dtype='datetime64[ns]', freq=None)

The result is a list of 3 equally-spaced days that range from the specified start date to the specified end date.

Example 3: Create Date Range with Specific Frequency

The following code shows how to create a date range that starts on a specific date and has a frequency of six month start dates:

import pandas as pd

#create date range with six month start dates
pd.date_range(start='1/1/2020', freq='MS', periods=6)

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

The result is a list 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.

The following code shows how to create a date range that starts on a specific date and has a yearly frequency:

import pandas as pd

#create date range with six consecutive years
pd.date_range(start='1/1/2020', freq='YS', periods=6)

DatetimeIndex(['2020-01-01', '2021-01-01', '2022-01-01', '2023-01-01',
               '2024-01-01', '2025-01-01'],
              dtype='datetime64[ns]', freq='AS-JAN')

The result is a list of six dates that are each one year apart.

Note: You can find the complete online documentation for the pd.date_range() function here.

Additional Resources

The following tutorials explain how to perform other common operations with dates in pandas:

How to Filter Pandas DataFrame Rows by Date
How to Sort a Pandas DataFrame by Date
How to Extract Month from Date in Pandas

You may also like