def date_range(start: Any = None,
end: Any = None,
periods: Any = None, # Number of periods to generate 要生成的期数
freq: Any = None, # 频率,默认值为' D '
tz: Any = None,
normalize: bool = False,
name: Hashable | None = None,
closed: str | None | NoDefault = lib.no_default,
inclusive: str | None = None, # 边界值是否包含 set each bound as closed or open
**kwargs: Any) -> DatetimeIndex
Return :a fixed frequency DatetimeIndex 返回一个固定频率的 DatetimeIndex 容器( An immutable container for datetimes)。
一、start
,end
,periods
, and freq
这四个参数中,如果 freq 没有的话,默认是 D(calendar day frequency),另外三个参数必须给出其中两个。
1. 指定start、end,得到的 DatetimeIndex 将获得一个从开始到结束时间(两边闭合)的一个线性间隔元素的期数。
date1 = pd.date_range(start='1/1/2018', end='1/08/2018') # freq 默认 D
print(date1)
# 返回
DatetimeIndex(['2018-01-01', '2018-01-02', '2018-01-03', '2018-01-04',
'2018-01-05', '2018-01-06', '2018-01-07', '2018-01-08'],
dtype='datetime64[ns]', freq='D')
2. 指定end、periods
pd.date_range(end='1/1/2018', periods=8)
# 返回
DatetimeIndex(['2017-12-25', '2017-12-26', '2017-12-27', '2017-12-28',
'2017-12-29', '2017-12-30', '2017-12-31', '2018-01-01'],
dtype='datetime64[ns]', freq='D')
3.指定start、end、periods
pd.date_range(start='2018-04-24', end='2018-04-27', periods=3)
# 返回
DatetimeIndex(['2018-04-24 00:00:00', '2018-04-25 12:00:00',
'2018-04-27 00:00:00'],
dtype='datetime64[ns]', freq=None)
频率是自动生成的(线性间隔)。the frequency is generated automatically (linearly spaced) 这里包含了首尾,中间取了一天。
4. 指定 freq
pd.date_range(start='1/1/2018', periods=5, freq='M')
DatetimeIndex(['2018-01-31', '2018-02-28', '2018-03-31', '2018-04-30',
'2018-05-31'],
dtype='datetime64[ns]', freq='M')
freq='M'( month end frequency) 取了每个月的最后一天。如果 freq = '3M' (Multiples are allowed) 或者写成 freq=pd.offsets.MonthEnd(3) 结果如下:
pd.date_range(start='1/1/2018', periods=5, freq='3M')
# 返回
DatetimeIndex(['2018-01-31', '2018-04-30', '2018-07-31', '2018-10-31','2019-01-31'],
dtype='datetime64[ns]', freq='3M')
再比如:freq="MS"(month start frequency)
pd.date_range("2020-01-06", "2020-04-03", freq="MS")
# 返回
DatetimeIndex(['2020-02-01', '2020-03-01', '2020-04-01'], dtype='datetime64[ns]', freq='MS')
如果开始时间(start)“2020-01-06”不符合 "每月 month start”这样的 frequency,它不是一个月的开始日期。那么,返回的时间戳将从下一个有效的时间戳开始。对于结束时间(end)也是如此,返回的时间戳将停止于前一个有效时间戳。
另外,freq还可以指定为“小时+分钟” ,如下“2H20min”
date1 = pd.date_range(start='2011-01-01 00:00:00', periods=10, freq="2H20min")
print(date1)
# 输出
DatetimeIndex(['2011-01-01 00:00:00', '2011-01-01 02:20:00',
'2011-01-01 04:40:00', '2011-01-01 07:00:00',
'2011-01-01 09:20:00', '2011-01-01 11:40:00',
'2011-01-01 14:00:00', '2011-01-01 16:20:00',
'2011-01-01 18:40:00', '2011-01-01 21:00:00'],
dtype='datetime64[ns]', freq='140T')
二、 inclusive
默认首尾值是都包含 “both”,还可以用 “neither”, “left”, “right” ,下面举个 inclusive = 'neither' 的例子:
date1 = pd.date_range(start='2017-01-01', end='2017-01-04', inclusive='neither')
print(date1)
# 返回
DatetimeIndex(['2017-01-02', '2017-01-03'], dtype='datetime64[ns]', freq='D')
a list of frequency aliases
Alias | Description |
---|---|
B | business day frequency |
C | custom business day frequency |
D | calendar day frequency |
W | weekly frequency |
M | month end frequency |
SM | semi-month end frequency (15th and end of month) |
BM | business month end frequency |
CBM | custom business month end frequency |
MS | month start frequency |
SMS | semi-month start frequency (1st and 15th) |
BMS | business month start frequency |
CBMS | custom business month start frequency |
Q | quarter end frequency |
BQ | business quarter end frequency |
QS | quarter start frequency |
BQS | business quarter start frequency |
A, Y | year end frequency |
BA, BY | business year end frequency |
AS, YS | year start frequency |
BAS, BYS | business year start frequency |
BH | business hour frequency |
H | hourly frequency |
T, min | minutely frequency |
S | secondly frequency |
L, ms | milliseconds |
U, us | microseconds |
N | nanoseconds |