Pandas date_range(): Generate Date and Time Ranges

pandas.date_range() generates a sequence of dates or timestamps and returns them as a DatetimeIndex. We can control the starting date, ending date, number of periods, frequency, timezone and whether the boundary dates are included.


Syntax of pandas.date_range() 🔝

pd.date_range(
    start=None,
    end=None,
    periods=None,
    freq=None,
    tz=None,
    normalize=False,
    name=None,
    inclusive='both',
    unit=None
)
ParameterPurpose
startStarting date or timestamp.
endEnding date or timestamp.
periodsNumber of timestamps to generate.
freqFrequency such as day, hour, month start or month end.
tzTimezone of the resulting DatetimeIndex.
normalizeNormalize start and end timestamps to midnight before generating the range.
nameName assigned to the resulting DatetimeIndex.
inclusiveControl whether start and end boundaries are included.
unitSet the datetime resolution such as seconds, milliseconds, microseconds or nanoseconds.

Common valid combinations are:

start + end
start + periods
end + periods
start + end + periods
start + periods + freq
end + periods + freq
start + end + freq

When start and end are supplied without periods, the usual default frequency is one calendar day.

Basic Date Range Using start and end 🔝

import pandas as pd

dates=pd.date_range(
    start='2020-04-20',
    end='2020-04-27'
)

print(dates)
Output
DatetimeIndex(['2020-04-20', '2020-04-21', '2020-04-22', '2020-04-23',
               '2020-04-24', '2020-04-25', '2020-04-26', '2020-04-27'],
              dtype='datetime64[us]', freq='D')

The default frequency is daily, represented by D. Both the starting and ending dates are included by default.

Generate a Fixed Number of Dates with periods 🔝

Use periods to specify how many timestamps should be generated.

start + periods

import pandas as pd

dates=pd.date_range(
    start='2026-09-01',
    periods=5
)

print(dates)
Output
DatetimeIndex(['2026-09-01', '2026-09-02', '2026-09-03',
               '2026-09-04', '2026-09-05'],
              dtype='datetime64[us]', freq='D')

end + periods

dates=pd.date_range(
    end='2026-09-05',
    periods=5
)

This generates five daily dates ending on 5 September 2026.

start + end + periods

If start, end and periods are supplied without a frequency, Pandas creates evenly spaced timestamps between the two boundaries.

dates=pd.date_range(
    start='2020-04-20',
    end='2020-04-27',
    periods=3
)

print(dates)
Output
DatetimeIndex(['2020-04-20 00:00:00',
               '2020-04-23 12:00:00',
               '2020-04-27 00:00:00'],
              dtype='datetime64[us]', freq=None)

The timestamps are evenly distributed between the start and end values. They are not automatically restricted to complete days.

Generate Dates with freq 🔝

The freq parameter determines the distance between consecutive timestamps.

dates=pd.date_range(
    start='2026-09-01',
    periods=5,
    freq='D'
)

Common frequency aliases include:

AliasFrequency
DCalendar day
BBusiness day
WWeek
MSMonth start
MEMonth end
QSQuarter start
QEQuarter end
YSYear start
YEYear end
hHour
minMinute
sSecond

See the Pandas frequency aliases section for more date and time frequencies.

Daily and Custom Day Frequencies 🔝

Every Day

dates=pd.date_range(
    start='2026-09-01',
    periods=5,
    freq='D'
)

Every Three Days

dates=pd.date_range(
    start='2026-09-01',
    periods=5,
    freq='3D'
)

print(dates)
Output
DatetimeIndex(['2026-09-01', '2026-09-04', '2026-09-07',
               '2026-09-10', '2026-09-13'],
              dtype='datetime64[us]', freq='3D')

Combine Days and Hours

dates=pd.date_range(
    start='2026-09-01',
    periods=4,
    freq='3D6h'
)

Each timestamp is 3 days and 6 hours after the previous one.

Generate Business Days 🔝

Use B to generate standard business days. Saturdays and Sundays are skipped.

import pandas as pd

dates=pd.date_range(
    start='2026-09-04',
    periods=5,
    freq='B'
)

print(dates)
Output
DatetimeIndex(['2026-09-04', '2026-09-07', '2026-09-08',
               '2026-09-09', '2026-09-10'],
              dtype='datetime64[us]', freq='B')

This standard business-day frequency excludes weekends. It does not automatically know every country's public holidays.

Month Start and Month End 🔝

Month Start with MS

dates=pd.date_range(
    start='2026-01-01',
    periods=6,
    freq='MS'
)

print(dates)
Output
DatetimeIndex(['2026-01-01', '2026-02-01', '2026-03-01',
               '2026-04-01', '2026-05-01', '2026-06-01'],
              dtype='datetime64[us]', freq='MS')

Month End with ME

dates=pd.date_range(
    start='2026-01-01',
    periods=6,
    freq='ME'
)

print(dates)
Output
DatetimeIndex(['2026-01-31', '2026-02-28', '2026-03-31',
               '2026-04-30', '2026-05-31', '2026-06-30'],
              dtype='datetime64[us]', freq='ME')

Year-End Frequency 🔝

Use YE for year end. An ending month can also be specified.

Year Ending in February

dates=pd.date_range(
    start='2010-04-20',
    end='2020-04-27',
    freq='YE-FEB'
)

print(dates)
Output
DatetimeIndex(['2011-02-28', '2012-02-29', '2013-02-28',
               '2014-02-28', '2015-02-28', '2016-02-29',
               '2017-02-28', '2018-02-28', '2019-02-28',
               '2020-02-29'],
              dtype='datetime64[us]', freq='YE-FEB')

The leap-year dates show 29 February where applicable.

Generate Hours, Minutes and Seconds 🔝

Hourly Range

dates=pd.date_range(
    start='2026-09-05 09:00',
    periods=5,
    freq='h'
)

print(dates)
Output
DatetimeIndex(['2026-09-05 09:00:00', '2026-09-05 10:00:00',
               '2026-09-05 11:00:00', '2026-09-05 12:00:00',
               '2026-09-05 13:00:00'],
              dtype='datetime64[us]', freq='h')

Every 5 Hours 30 Minutes

dates=pd.date_range(
    start='2026-09-05 00:00',
    periods=5,
    freq='5h30min'
)

print(dates)
Output
DatetimeIndex(['2026-09-05 00:00:00', '2026-09-05 05:30:00',
               '2026-09-05 11:00:00', '2026-09-05 16:30:00',
               '2026-09-05 22:00:00'],
              dtype='datetime64[us]', freq='330min')

Every Minute

dates=pd.date_range(
    start='2026-09-05 10:00',
    periods=5,
    freq='min'
)

Every Second

dates=pd.date_range(
    start='2026-09-05 10:00',
    periods=5,
    freq='s'
)

Generate Dates Using Today's Date 🔝

Timestamp.today() can supply the current local date and time.

import pandas as pd

today=pd.Timestamp.today().normalize()

dates=pd.date_range(
    end=today,
    periods=10,
    freq='D'
)

print(dates)

This creates exactly ten dates ending with today. Using normalize() changes the current timestamp to midnight before the range is generated.

Next Five Hours from the Current Time

import pandas as pd

now=pd.Timestamp.now()

dates=pd.date_range(
    start=now,
    periods=5,
    freq='h'
)

print(dates)

The actual output depends on the time at which the program is run.

Convert date_range() to a Python List 🔝

date_range() returns a DatetimeIndex. Use tolist() to convert it to a Python list.

import pandas as pd

dates=pd.date_range(
    start='2026-09-01',
    periods=5
)

date_list=dates.tolist()

print(date_list)

The resulting list contains Pandas Timestamp objects.

Convert to Date Strings

date_strings=dates.strftime(
    '%Y-%m-%d'
).tolist()

print(date_strings)
Output
['2026-09-01', '2026-09-02', '2026-09-03',
 '2026-09-04', '2026-09-05']

Include or Exclude Start and End Dates 🔝

The inclusive parameter controls whether the start and end boundaries are included.

The available values are:

'both'
'left'
'right'
'neither'

Include Both Boundaries

dates=pd.date_range(
    start='2026-09-01',
    end='2026-09-05',
    inclusive='both'
)
Output
2026-09-01
2026-09-02
2026-09-03
2026-09-04
2026-09-05

Exclude the Start Date

dates=pd.date_range(
    start='2026-09-01',
    end='2026-09-05',
    inclusive='right'
)
Output
2026-09-02
2026-09-03
2026-09-04
2026-09-05

Exclude the End Date

dates=pd.date_range(
    start='2026-09-01',
    end='2026-09-05',
    inclusive='left'
)
Output
2026-09-01
2026-09-02
2026-09-03
2026-09-04

Exclude Both Boundaries

dates=pd.date_range(
    start='2026-09-01',
    end='2026-09-05',
    inclusive='neither'
)
Output
2026-09-02
2026-09-03
2026-09-04

Normalize Time to Midnight 🔝

With normalize=True, the start and end values are normalized to midnight before the date range is generated.

import pandas as pd

dates=pd.date_range(
    start='2026-09-05 14:35:20',
    periods=3,
    freq='D',
    normalize=True
)

print(dates)
Output
DatetimeIndex(['2026-09-05', '2026-09-06', '2026-09-07'],
              dtype='datetime64[us]', freq='D')

Without normalization, the time 14:35:20 would be retained in each generated timestamp.

Create a Timezone-Aware Date Range with tz 🔝

The tz parameter creates a timezone-aware DatetimeIndex.

import pandas as pd

dates=pd.date_range(
    start='2026-09-05 09:00',
    periods=3,
    freq='4h',
    tz='Asia/Kolkata'
)

print(dates)
Output
DatetimeIndex(['2026-09-05 09:00:00+05:30',
               '2026-09-05 13:00:00+05:30',
               '2026-09-05 17:00:00+05:30'],
              dtype='datetime64[us, Asia/Kolkata]', freq='4h')

Give the DatetimeIndex a Name 🔝

The name parameter assigns a name to the resulting DatetimeIndex.

dates=pd.date_range(
    start='2026-09-01',
    periods=3,
    name='report_date'
)

print(dates.name)
Output
report_date

The name parameter does not convert the result into a DataFrame. The result remains a DatetimeIndex.

Set Datetime Resolution with unit 🔝

The unit parameter can request a particular datetime resolution. Supported values include seconds, milliseconds, microseconds and nanoseconds.

dates=pd.date_range(
    start='2026-09-05 10:00:00',
    periods=3,
    freq='h',
    unit='s'
)

print(dates.dtype)
Output
datetime64[s]

How Frequency Boundaries Affect the Result 🔝

For anchored frequencies such as month start or month end, the starting value itself may not be a valid timestamp for that frequency.

For example:

dates=pd.date_range(
    start='2026-01-15',
    periods=3,
    freq='MS'
)

print(dates)
Output
DatetimeIndex(['2026-02-01', '2026-03-01', '2026-04-01'],
              dtype='datetime64[us]', freq='MS')

15 January is not a month-start boundary, so the first valid MS timestamp is 1 February.

Similarly, with ME, Pandas moves to the next valid month-end timestamp when necessary.

Current Pandas Frequency Aliases 🔝

Several older frequency aliases have changed. When writing new code, prefer the current forms below.

Current AliasMeaningOlder Alias to Avoid
hHourH
minMinuteT
MEMonth endM
QEQuarter endQ
YEYear endY
YE-FEBYear ending in FebruaryA-FEB

Aliases such as D, B, W, MS, YS, s, ms, us and ns continue to represent their corresponding frequencies.

Common Mistakes with pandas.date_range() 🔝

1. Calling DataFrame.date_range()

Incorrect:

df.date_range(...)

Correct:

pd.date_range(...)

2. Using Old Frequency Aliases

For current Pandas, avoid old code such as:

freq='H'
freq='T'
freq='M'
freq='A-FEB'

Use:

freq='h'
freq='min'
freq='ME'
freq='YE-FEB'

3. Using closed Instead of inclusive

Old:

closed='right'

Current:

inclusive='right'

4. Expecting periods to Mean Days

periods means the number of generated timestamps. Their distance depends on freq.

pd.date_range(
    start='2026-09-01',
    periods=3,
    freq='ME'
)

This generates three month-end timestamps, not three days.

5. Supplying Too Many Conflicting Parameters

The function determines the range from combinations of start, end, periods and freq. Do not try to independently fix all four values when they conflict.

6. Assuming an Anchored Frequency Always Starts on start

For frequencies such as MS or ME, the first result must be a valid boundary for that frequency.

Summary of pandas.date_range() 🔝

  • pd.date_range() creates a DatetimeIndex.
  • It is a Pandas function, not a DataFrame method.
  • Use start and end to define date boundaries.
  • Use periods to control how many timestamps are generated.
  • Use freq to control the distance between timestamps.
  • D generates calendar days and B standard business days.
  • Use MS for month start and ME for month end.
  • Use YS for year start and YE for year end.
  • Use lowercase h for hours and min for minutes.
  • Multiple frequency units can be combined, such as 5h30min.
  • inclusive controls whether start and end boundaries are included.
  • normalize=True resets the start and end times to midnight before generating the range.
  • tz creates a timezone-aware DatetimeIndex.
  • name assigns a name to the DatetimeIndex.
  • unit controls datetime resolution where supported.
  • tolist() converts the DatetimeIndex to a Python list.
  • Anchored frequencies such as MS and ME start from the next valid boundary when necessary.
  • Older aliases such as H, T, M, Y and A-FEB should not be used in current Pandas date_range() examples.
to_datetime() period_range() DateOffset() strftime()




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer