pandas date_range()

generate DatetimeIndex sequences quickly and easily

Posted by Rpl on June 12, 2019

you can click upon botton 中文 Chinese to see the chinese version

Paramters


pandas.date_range(start=None, end=None, periods=None, freq=None,  tz=None, normalize=False, name=None, closed=None, **kwargs)

Return a fixed frequency DatetimeIndex.

paramters  
start str or datetime-like, optional, Left bound for generating dates.
end str or datetime-like, optional, Right bound for generating dates.
periods integer, optional, Number of periods to generate.
freq str or DateOffset, default ‘D’ Frequency strings can have multiples, e.g. ‘5H’. See here for a list of frequency aliases.
tz str or tzinfo, optional, Time zone name for returning localized DatetimeIndex, for example ‘Asia/Hong_Kong’. By default, the resulting DatetimeIndex is timezone-naive.
normalize bool, default False, Normalize start/end dates to midnight before generating date range.
name str, default None, Name of the resulting DatetimeIndex.
closed {None, ‘left’, ‘right’}, optional, Make the interval closed with respect to the given frequency to the ‘left’, ‘right’, or both sides (None, the default).
**kwrags For compatibility. Has no effect on the result.

Notes


Of the four parameters start, end, periods, and freq, exactly three must be specified. If freq is omitted, the resulting DatetimeIndex will have periods linearly spaced elements between start and end (closed on both sides).

To learn more about the frequency strings, please see this link.

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.

1
2
3
4
5
6
pd.date_range(start='20180102', end='20180109')

# pd.date_range(start=''1/1/2018’', end='1/9/2018')
DatetimeIndex(['2018-01-02', '2018-01-03', '2018-01-04', '2018-01-05',
               '2018-01-06', '2018-01-07', '2018-01-08', '2018-01-09'],
              dtype='datetime64[ns]', freq='D')

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

1
2
3
4
5
pd.date_range(start='20180102', periods=8)

DatetimeIndex(['2018-01-02', '2018-01-03', '2018-01-04', '2018-01-05',
               '2018-01-06', '2018-01-07', '2018-01-08', '2018-01-09'],
              dtype='datetime64[ns]', freq='D')

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

1
2
3
4
5
6
pd.date_range(end='20180109', periods=8)

DatetimeIndex(['2018-01-02', '2018-01-03', '2018-01-04', '2018-01-05',
               '2018-01-06', '2018-01-07', '2018-01-08', '2018-01-09'],
              dtype='datetime64[ns]', freq='D')

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

1
2
3
4
5
pd.date_range(start='20180102', end='20180109', periods=3)

DatetimeIndex(['2018-01-02 00:00:00', '2018-01-05 12:00:00',
               '2018-01-09 00:00:00'],
              dtype='datetime64[ns]', freq=None)
Other Parameters

Changed the freq (frequency) to ’M’ (month end frequency).

1
2
3
4
5
pd.date_range(start='1/1/2019', periods=5, freq='M')

DatetimeIndex(['2019-01-31', '2019-02-28', '2019-03-31', '2019-04-30',
               '2019-05-31'],
              dtype='datetime64[ns]', freq='M')

Multiples are allowed

1
2
3
4
5
pd.date_range(start='1/1/2019', periods=5, freq='3M')

DatetimeIndex(['2019-01-31', '2019-04-30', '2019-07-31', '2019-10-31',
               '2020-01-31'],
              dtype='datetime64[ns]', freq='3M')

freq can also be specified as an Offset object.

1
2
3
4
5
pd.date_range(start='/1/1/2019', periods=5, freq=pd.offsets.MonthEnd(3))

DatetimeIndex(['2019-01-31', '2019-04-30', '2019-07-31', '2019-10-31',
               '2020-01-31'],
              dtype='datetime64[ns]', freq='3M')

Specify tz to set the timezone.

1
2
3
4
5
6
pd.date_range(start='/1/1/2019', periods=5, tz='Asia/Tokyo')

DatetimeIndex(['2019-01-01 00:00:00+09:00', '2019-01-02 00:00:00+09:00',
               '2019-01-03 00:00:00+09:00', '2019-01-04 00:00:00+09:00',
               '2019-01-05 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.

1
2
3
4
5
pd.date_range(start='1/1/2019', end='1/6/2019', closed=None)

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

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

1
2
3
4
5
pd.date_range(start='1/1/2019', end='1/6/2019', closed='left')

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

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

1
2
3
4
5
pd.date_range(start='1/1/2019', end='1/6/2019', closed='right')

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

前言


pandas中的date_range()函数用来生成一个日期序列,在需要构造一个日期序列的时候非常方便。 本篇文章是对英文文档的翻译,英文版点击上面的英文 English查看,或者点击这里查看网页原文

参数解释


pandas.date_range(start=None, end=None, periods=None, freq=None,  tz=None, normalize=False, name=None, closed=None, **kwargs)

此函数返回一个固定频率的DatetimeIndex类型的数据(就是返回一个时间序列)

start:生成日期序列的左区间,str或datetime-like类型, 参数可选。

end: 生成日期序列的右区间,str或datetime-like类型, 参数可选。

periods:要生成的周期数,周期是指生成的序列的长度, int整型, 参数可选。

freq:要生成的日期的频次, 频次是指两个相邻日期间隔时间, 可以是5小时:”5H”, 1天:”D”, 3个月:”3M”等等, 点击查看具体频次别名表

tz:时区,返回所选时区的日期时间,如:’Asia/Hong_Kong’,默认生成的日期序列与时区无关,str类型,参数可选。

normalize:在生成日期序列之前,将start/end标准化为午夜。 bool类型,默认False。

name: 生成的DatetimeIndex序列的名字, str类型,默认None。

closed:左右日期区间是否闭合可取。’left’:左闭右开,’right’:左开右闭。默认:None, 左右都是闭区间。

**kwargs:向后兼容性考虑,对结果没有影响。

注意


start,end,periods,freq四个参数中只有三个需要明确指定。如果freq被忽略,则生成的日期序列的periods(天数)是线性间隔的,包括开始和结束日期。(具体看例子就明白了)

实例


日期参数实例

以下的四个例子都生成了相同的日期序列,但使用了不同的start, end, periods组合。

  1. 指定start和end, freq默认为days
1
2
3
4
5
6
7
pd.date_range(start='20180102', end='20180109')

# 或者 pd.date_range(start=''1/1/2018’', end='1/9/2018')
DatetimeIndex(['2018-01-02', '2018-01-03', '2018-01-04', '2018-01-05',
               '2018-01-06', '2018-01-07', '2018-01-08', '2018-01-09'],
              dtype='datetime64[ns]', freq='D')

2.指定start和periods, periods周期数是天

1
2
3
4
5
pd.date_range(start='20180102', periods=8)

DatetimeIndex(['2018-01-02', '2018-01-03', '2018-01-04', '2018-01-05',
               '2018-01-06', '2018-01-07', '2018-01-08', '2018-01-09'],
              dtype='datetime64[ns]', freq='D')

3.指定end和periods

1
2
3
4
5
pd.date_range(end='20180109', periods=8)

DatetimeIndex(['2018-01-02', '2018-01-03', '2018-01-04', '2018-01-05',
               '2018-01-06', '2018-01-07', '2018-01-08', '2018-01-09'],
              dtype='datetime64[ns]', freq='D')

4.指定start,end, periods。freq频次自动

1
2
3
4
5
pd.date_range(start='20180102', end='20180109', periods=3)

DatetimeIndex(['2018-01-02 00:00:00', '2018-01-05 12:00:00',
               '2018-01-09 00:00:00'],
              dtype='datetime64[ns]', freq=None)
其他参数实例

将freq频率参数改为’M’(月末频率), 默认是’D’(天)

1
2
3
4
5
pd.date_range(start='1/1/2019', periods=5, freq='M')

DatetimeIndex(['2019-01-31', '2019-02-28', '2019-03-31', '2019-04-30',
               '2019-05-31'],
              dtype='datetime64[ns]', freq='M')

频率可以多倍数表示

1
2
3
4
5
pd.date_range(start='1/1/2019', periods=5, freq='3M')

DatetimeIndex(['2019-01-31', '2019-04-30', '2019-07-31', '2019-10-31',
               '2020-01-31'],
              dtype='datetime64[ns]', freq='3M')

freq也可以指定为偏移量对象

1
2
3
4
5
pd.date_range(start='/1/1/2019', periods=5, freq=pd.offsets.MonthEnd(3))

DatetimeIndex(['2019-01-31', '2019-04-30', '2019-07-31', '2019-10-31',
               '2020-01-31'],
              dtype='datetime64[ns]', freq='3M')

指定tz来设置时区

1
2
3
4
5
6
pd.date_range(start='/1/1/2019', periods=5, tz='Asia/Tokyo')

DatetimeIndex(['2019-01-01 00:00:00+09:00', '2019-01-02 00:00:00+09:00',
               '2019-01-03 00:00:00+09:00', '2019-01-04 00:00:00+09:00',
               '2019-01-05 00:00:00+09:00'],
              dtype='datetime64[ns, Asia/Tokyo]', freq='D')

参数closed控制是否包含start和end的边界,默认是包含这两个端点的。(类似区间两端点的开闭)

1
2
3
4
5
pd.date_range(start='1/1/2019', end='1/6/2019', closed=None)

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

closed=’left’,左闭右开,只包含start端点,不包含end端点

1
2
3
4
5
pd.date_range(start='1/1/2019', end='1/6/2019', closed='left')

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

closed=’right’, 左开右闭,不包含start端点,但包含end端点

1
2
3
4
5
pd.date_range(start='1/1/2019', end='1/6/2019', closed='right')

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