How do you create a date range in pandas?

How do you create a date range in pandas?

Creating a date range in pandas involves using the pd.date_range() function. This function allows you to specify a start date, end date, and frequency (such as daily, weekly, or monthly) to generate a range of dates within that timeframe. The resulting date range can be used for various operations and analysis within a pandas dataframe.

Create a Date Range in Pandas (3 Examples)


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

Additional Resources

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

Cite this article

stats writer (2024). How do you create a date range in pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-you-create-a-date-range-in-pandas/

stats writer. "How do you create a date range in pandas?." PSYCHOLOGICAL SCALES, 2 Jul. 2024, https://scales.arabpsychology.com/stats/how-do-you-create-a-date-range-in-pandas/.

stats writer. "How do you create a date range in pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-you-create-a-date-range-in-pandas/.

stats writer (2024) 'How do you create a date range in pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-you-create-a-date-range-in-pandas/.

[1] stats writer, "How do you create a date range in pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.

stats writer. How do you create a date range in pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)

Comments are closed.

Slide Up
x
PDF
Scroll to Top