Pandas date_range(): Generate Dates and Times

Generate timestamp sequences with calendar and business frequencies, periods, boundary inclusion, timezones and datetime resolution.

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.

At most three of start, end, periods and freq may be supplied. Specify at least two of start, end and periods. With start/end/periods and no freq, points are spaced evenly.

Basic Date Range Using start and end 🔝

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

import pandas as pd

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

print(dates)

Expected 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')

Generate a Fixed Number of Dates with periods 🔝

Use periods to specify how many timestamps should be generated.

start + periods

end + periods

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.

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

import pandas as pd

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

print(dates)

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

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

print(dates)

Expected output

DatetimeIndex(['2026-09-01', '2026-09-02', '2026-09-03', '2026-09-04',
               '2026-09-05'],
              dtype='datetime64[us]', freq='D')
DatetimeIndex(['2026-09-01', '2026-09-02', '2026-09-03', '2026-09-04',
               '2026-09-05'],
              dtype='datetime64[us]', freq='D')
DatetimeIndex(['2020-04-20 00:00:00', '2020-04-23 12:00:00',
               '2020-04-27 00:00:00'],
              dtype='datetime64[us]', freq=None)

Generate Dates with freq 🔝

The freq parameter determines the distance between consecutive timestamps.

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.

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

Expected output

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

Daily and Custom Day Frequencies 🔝

Every Day

Every Three Days

Combine Days and Hours

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

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

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

print(dates)

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

Expected output

DatetimeIndex(['2026-09-01', '2026-09-02', '2026-09-03', '2026-09-04',
               '2026-09-05'],
              dtype='datetime64[us]', freq='D')
DatetimeIndex(['2026-09-01', '2026-09-04', '2026-09-07', '2026-09-10',
               '2026-09-13'],
              dtype='datetime64[us]', freq='3D')
DatetimeIndex(['2026-09-01 00:00:00', '2026-09-04 06:00:00',
               '2026-09-07 12:00:00', '2026-09-10 18:00:00'],
              dtype='datetime64[us]', freq='78h')

Generate Business Days 🔝

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

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

B excludes weekends; public holidays are not automatically excluded. Use a custom business calendar when needed.

import pandas as pd

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

print(dates)

Expected output

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

Month Start and Month End 🔝

Month Start with MS

Month End with ME

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

print(dates)

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

print(dates)

Expected 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')
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

The leap-year dates show 29 February where applicable.

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

print(dates)

Expected 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')

Generate Hours, Minutes and Seconds 🔝

Hourly Range

Every 5 Hours 30 Minutes

Every Minute

Every Second

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

print(dates)

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

print(dates)

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

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

Expected 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')
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')
DatetimeIndex(['2026-09-05 10:00:00', '2026-09-05 10:01:00',
               '2026-09-05 10:02:00', '2026-09-05 10:03:00',
               '2026-09-05 10:04:00'],
              dtype='datetime64[us]', freq='min')
DatetimeIndex(['2026-09-05 10:00:00', '2026-09-05 10:00:01',
               '2026-09-05 10:00:02', '2026-09-05 10:00:03',
               '2026-09-05 10:00:04'],
              dtype='datetime64[us]', freq='s')

Generate Dates Using Today's Date 🔝

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

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

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

These two examples execute the actual local clock calls; dates change on each run. Only counts are shown for repeatable validation.

import pandas as pd

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

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

print("Generated timestamps:", len(dates))
assert len(dates) in (5, 10)

import pandas as pd

now=pd.Timestamp.now()

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

print("Generated timestamps:", len(dates))
assert len(dates) in (5, 10)

Expected output

Generated timestamps: 10
Generated timestamps: 5

Convert date_range() to a Python List 🔝

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

The resulting list contains Pandas Timestamp objects.

Convert to Date Strings

import pandas as pd

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

date_list=dates.tolist()

print(date_list)

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

print(date_strings)

Expected output

[Timestamp('2026-09-01 00:00:00'), Timestamp('2026-09-02 00:00:00'), Timestamp('2026-09-03 00:00:00'), Timestamp('2026-09-04 00:00:00'), Timestamp('2026-09-05 00:00:00')]
['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:

Include Both Boundaries

Exclude the Start Date

Exclude the End Date

Exclude Both Boundaries

dates=pd.date_range(
    start='2026-09-01',
    end='2026-09-05',
    inclusive='both'
)
print(dates)

dates=pd.date_range(
    start='2026-09-01',
    end='2026-09-05',
    inclusive='right'
)
print(dates)

dates=pd.date_range(
    start='2026-09-01',
    end='2026-09-05',
    inclusive='left'
)
print(dates)

dates=pd.date_range(
    start='2026-09-01',
    end='2026-09-05',
    inclusive='neither'
)
print(dates)

Expected output

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

Normalize Time to Midnight 🔝

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

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

import pandas as pd

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

print(dates)

Expected output

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

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)

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

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

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

print(dates.name)

Expected output

report_date

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)

Expected 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:

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.

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

print(dates)

Expected output

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

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.

Exercise: make a month-end report 🔝

Create three month ends from 15 January 2026. Add a synthetic sales column and keep dates as the index.

Exercise solution 🔝

Use ME to align to calendar month ends.
index = pd.date_range("2026-01-15", periods=3, freq="ME", name="report_date")
report = pd.DataFrame({"sales": [100, 125, 110]}, index=index)
print(report)
assert report.index.strftime("%Y-%m-%d").tolist() == ["2026-01-31", "2026-02-28", "2026-03-31"]
assert report["sales"].sum() == 335

Expected output

             sales
report_date       
2026-01-31     100
2026-02-28     125
2026-03-31     110

Practice in Google Colab 🔝

Open in Google Colab View on GitHub

Run examples in order, inspect formatted dates and try the exercise. Save a copy to keep edits.

Reference: Pandas date_range() documentation.




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