date_range()是pandas中常用的函数,用于生成一个固定频率的DatetimeIndex时间索引。原型:
date_range(start=None, end=None, periods=None, freq=None, tz=None, normalize=False, name=None, closed=None, **kwargs)
常用参数为start、end、periods、freq。
start:指定生成时间序列的开始时间
end:指定生成时间序列的结束时间
periods:指定生成时间序列的数量
freq:生成频率,默认‘D’,可以是’H’、‘D’、‘M’、‘5H’、‘10D’、…
还可以根据closed参数选择是否包含开始和结束时间,left包含开始时间,不包含结束时间,right与之相反。默认同时包含开始时间和结束时间。
函数调用时至少要指定参数start、end、periods中的两个。
(1)指定起止时间
pd.date_range('20200101','20200110')
(2)指定开始时间和时间序列数量
pd.date_range('20200101',periods=10)