Pandas date_range()

The date_range() method in Pandas is used to generate a fixed-frequency DatetimeIndex.

It is commonly used in time series data analysis, as it allows for the creation of a range of date points using various parameters provided by the date_range() function.

Example

import pandas as pd

# create a date range dr = pd.date_range(start='2020-01-01', end='2020-01-10')
print(dr) ''' Output 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') '''

date_range() Syntax

The syntax of the date_range() method in Pandas is:

Pandas.date_range(start=None, end=None, periods=None, freq=None, tz=None, name=None, **kwargs)

date_range() Arguments

The date_range() method takes following arguments:

  • start - left bound for generating dates
  • end - right bound for generating dates
  • periods (optional) - number of periods to generate
  • freq (optional) - specifies the frequency of the generated dates
  • tz (optional) - time zone name for returning localized DatetimeIndex.
  • name (optional) - name for the resulting DateTimeIndex
  • kwargs (optional) - the unit of the arg for epoch times.

date_range() Return Value

The date_range() method returns a DateTimeIndex of fixed frequency.


Example 1: Create Range of Date Using date_range()

import pandas as pd

# create date range dr = pd.date_range(start='2023-12-01', end='2023-12-05')
print(dr)

Output

DatetimeIndex(['2023-12-01', '2023-12-02', '2023-12-03', '2023-12-04',
               '2023-12-05'],
              dtype='datetime64[ns]', freq='D')

In the above example, we have used the pd.date_range() method to create a range of dates.

This method generates a sequence of dates starting from 2023-12-01 to 2023-12-05.


Example 2: Create a Date Range With Specified Number of Periods

import pandas as pd

# create a date range with a specified number of periods dr = pd.date_range(start='2020-01-01', periods=15)
print(dr)

Output

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', '2020-01-11', '2020-01-12',
               '2020-01-13', '2020-01-14', '2020-01-15'],
              dtype='datetime64[ns]', freq='D')

In this example, we have used the date_range() method to create a range of dates. We specified the start date as 2020-01-01.

The periods argument is set to 15, which means the function will generate a total of 15 dates.

Note: Since no frequency freq is specified, it defaults to D (daily), so the dates will be consecutive days starting from the start date.


Example 3: Date Range With a Specific Frequency

import pandas as pd

# create a date range with a specific frequency dr = pd.date_range(start='2020-01-01', end='2020-03-01', freq='W-SUN')
print(dr)

Output

DatetimeIndex(['2020-01-05', '2020-01-12', '2020-01-19', '2020-01-26',
               '2020-02-02', '2020-02-09', '2020-02-16', '2020-02-23',
               '2020-03-01'],
              dtype='datetime64[ns]', freq='W-SUN')

Here, date_range() is used to create a range of dates with the start date set to 2020-01-01 and the end date is 2020-03-01.

The freq argument is set to W-SUN. This means the dates will be generated with a weekly frequency, specifically on Sundays.


Example 4: Set Timezone for Generated Date Range

import pandas as pd

# create a date range with a specified timezone dr = pd.date_range(start='2020-01-01', end='2020-01-10', tz='US/Eastern')
print(dr)

Output

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

In this example, the tz argument is specified as US/Eastern. This assigns the Eastern Time Zone to the generated dates.


Example 5: Use of unit Argument in date_range()

import pandas as pd

# create a date range with a name assigned to the DatetimeIndex dr = pd.date_range(start='2020-01-01', end='2020-01-10', name='January_2020')
print(dr)

Output

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]', name='January_2020', freq='D')

Here, the name argument is set to January_2020. This name is assigned to the DatetimeIndex generated by date_range().

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges