Examples

Specifying the values
The next four examples generate the same DatetimeIndex, but vary the combination of start, end and periods.
Specify start and end, with the default daily frequency.

In [1]:
import numpy as np
import pandas as pd
In [3]:
pd.date_range(start='2/2/2019', end='2/08/2019')
Out[3]:
DatetimeIndex(['2019-02-02', '2019-02-03', '2019-02-04', '2019-02-05',
               '2019-02-06', '2019-02-07', '2019-02-08'],
              dtype='datetime64[ns]', freq='D')

Specify start and periods, the number of periods (days).

In [5]:
pd.date_range(start='2/2/2019', periods=8)
Out[5]:
DatetimeIndex(['2019-02-02', '2019-02-03', '2019-02-04', '2019-02-05',
               '2019-02-06', '2019-02-07', '2019-02-08', '2019-02-09'],
              dtype='datetime64[ns]', freq='D')

Specify end and periods, the number of periods (days).

In [6]:
pd.date_range(end='2/2/2019', periods=8)
Out[6]:
DatetimeIndex(['2019-01-26', '2019-01-27', '2019-01-28', '2019-01-29',
               '2019-01-30', '2019-01-31', '2019-02-01', '2019-02-02'],
              dtype='datetime64[ns]', freq='D')

Specify start, end, and periods; the frequency is generated automatically (linearly spaced).

In [8]:
pd.date_range(start='2019-06-20', end='2019-04-25', periods=4)
Out[8]:
DatetimeIndex(['2019-06-20 00:00:00', '2019-06-01 08:00:00',
               '2019-05-13 16:00:00', '2019-04-25 00:00:00'],
              dtype='datetime64[ns]', freq=None)

Other Parameters
Changed the freq (frequency) to 'M' (month end frequency).

In [9]:
pd.date_range(start='2/2/2019', periods=6, freq='M')
Out[9]:
DatetimeIndex(['2019-02-28', '2019-03-31', '2019-04-30', '2019-05-31',
               '2019-06-30', '2019-07-31'],
              dtype='datetime64[ns]', freq='M')

Multiples are allowed

In [10]:
pd.date_range(start='2/2/2019', periods=6, freq='3M')
Out[10]:
DatetimeIndex(['2019-02-28', '2019-05-31', '2019-08-31', '2019-11-30',
               '2020-02-29', '2020-05-31'],
              dtype='datetime64[ns]', freq='3M')

freq can also be specified as an Offset object.

In [11]:
pd.date_range(start='2/2/2019', periods=6, freq=pd.offsets.MonthEnd(3))
Out[11]:
DatetimeIndex(['2019-02-28', '2019-05-31', '2019-08-31', '2019-11-30',
               '2020-02-29', '2020-05-31'],
              dtype='datetime64[ns]', freq='3M')

Specify tz to set the timezone.

In [12]:
pd.date_range(start='2/2/2019', periods=6, tz='Asia/Tokyo')
Out[12]:
DatetimeIndex(['2019-02-02 00:00:00+09:00', '2019-02-03 00:00:00+09:00',
               '2019-02-04 00:00:00+09:00', '2019-02-05 00:00:00+09:00',
               '2019-02-06 00:00:00+09:00', '2019-02-07 00:00:00+09:00'],
              dtype='datetime64[ns, Asia/Tokyo]', freq='D')

closed controls whether to include start and end that are on the boundary. The default includes boundary points
on either end.

In [13]:
pd.date_range(start='2018-02-02', end='2018-02-05', closed=None)
Out[13]:
DatetimeIndex(['2018-02-02', '2018-02-03', '2018-02-04', '2018-02-05'], dtype='datetime64[ns]', freq='D')

Use closed='left' to exclude end if it falls on the boundary.

In [14]:
pd.date_range(start='2018-02-02', end='2018-02-05', closed='left')
Out[14]:
DatetimeIndex(['2018-02-02', '2018-02-03', '2018-02-04'], dtype='datetime64[ns]', freq='D')

Use closed='right' to exclude start if it falls on the boundary.

In [15]:
pd.date_range(start='2018-02-02', end='2018-02-05', closed='right')
Out[15]:
DatetimeIndex(['2018-02-03', '2018-02-04', '2018-02-05'], dtype='datetime64[ns]', freq='D')