一、介绍
类似于 range
产生等差数列,date_range
产生的是等差时间序列。
生成一个固定频率的时间索引,必须指定 start
、end
、periods
中的两个参数值,否则报错。
使用语法:
pandas.date_range(start=None, end=None, periods=None,
freq=None, tz=None, normalize=False,
name=None, closed=None, **kwargs)
参数说明:
start -- 开始时间
end -- 结束时间
periods -- 总数量长度
freq -- 时间间隔、日期偏移量 默认'D'
tz -- 时区
normalize -- 是否标准化到 midnight 午夜时间戳
name -- 列名称
closed -- 首尾是否在内 'left'、'right'
二、实操
- 指定开始、结束时间
import pandas as pd
# 指定开始、结束
pd.date_range(start='20211001', end='20211010')
'''
DatetimeIndex(['2021-10-01', '2021-10-02', '2021-10-03', '2021-10-04',
'2021-10-05', '2021-10-06', '2021-10-07', '2021-10-08',
'2021-10-09', '2021-10-10'],
dtype='datetime64[ns]', freq='D')
'''
- 指定个数
# 指定个数
pd.date_range(start='2021-10-01', periods=5)
'''
DatetimeIndex(['2021-10-01', '2021-10-02', '2021-10-03', '2021-10-04',
'2021-10-05'],
dtype='datetime64[ns]', freq='D')
'''
- 指定频率(间隔)
freq
可以传入所有 Offset Aliases
。
# 指定频率(间隔)
pd.date_range(start='2021-10-01', periods=10, freq='1D')
'''
DatetimeIndex(['2021-10-01', '2021-10-02', '2021-10-03', '2021-10-04',
'2021-10-05', '2021-10-06', '2021-10-07', '2021-10-08',
'2021-10-09', '2021-10-10'],
dtype='datetime64[ns]', freq='D')
'''
pd.date_range(start='2021-10-01', end='2021-10-10', freq='3D')
'''
DatetimeIndex(['2021-10-01', '2021-10-04', '2021-10-07', '2021-10-10'], dtype='datetime64[ns]', freq='3D')
'''
import pandas as pd
pd.date_range('20211018', periods=10, freq='5H')
'''
DatetimeIndex(['2021-10-18 00:00:00', '2021-10-18 05:00:00',
'2021-10-18 10:00:00', '2021-10-18 15:00:00',
'2021-10-18 20:00:00', '2021-10-19 01:00:00',
'2021-10-19 06:00:00', '2021-10-19 11:00:00',
'2021-10-19 16:00:00', '2021-10-19 21:00:00'],
dtype='datetime64[ns]', freq='5H')
'''
pd.date_range('20211018', periods=10, freq='3M')
'''
DatetimeIndex(['2021-10-31', '2022-01-31', '2022-04-30', '2022-07-31',
'2022-10-31', '2023-01-31', '2023-04-30', '2023-07-31',
'2023-10-31', '2024-01-31'],
dtype='datetime64[ns]', freq='3M')
'''
- business day(工作日)实现
pd.date_range('20211018', freq='B', periods=10)
'''
DatetimeIndex(['2021-10-18', '2021-10-19', '2021-10-20', '2021-10-21',
'2021-10-22', '2021-10-25', '2021-10-26', '2021-10-27',
'2021-10-28', '2021-10-29'],
dtype='datetime64[ns]', freq='B')
'''
pd.bdate_range('20211018', periods=10) # 结果同上
- 是否标准化到午夜时间戳
# 从0点开始
pd.date_range(start='2021-10-01 17:23:10', periods=10, freq='s', normalize=True)
'''
DatetimeIndex(['2021-10-01 00:00:00', '2021-10-01 00:00:01',
'2021-10-01 00:00:02', '2021-10-01 00:00:03',
'2021-10-01 00:00:04', '2021-10-01 00:00:05',
'2021-10-01 00:00:06', '2021-10-01 00:00:07',
'2021-10-01 00:00:08', '2021-10-01 00:00:09'],
dtype='datetime64[ns]', freq='S')
'''
# 从指定时间开始
pd.date_range(start='2021-10-01 17:23:10', periods=10, freq='s')
'''
DatetimeIndex(['2021-10-01 17:23:10', '2021-10-01 17:23:11',
'2021-10-01 17:23:12', '2021-10-01 17:23:13',
'2021-10-01 17:23:14', '2021-10-01 17:23:15',
'2021-10-01 17:23:16', '2021-10-01 17:23:17',
'2021-10-01 17:23:18', '2021-10-01 17:23:19'],
dtype='datetime64[ns]', freq='S')
'''
- 左右开区间、闭区间
# 左闭右开
pd.date_range(start='2021-10-01', end='2021-10-10', freq='3D', closed='left')
# DatetimeIndex(['2021-10-01', '2021-10-04', '2021-10-07'], dtype='datetime64[ns]', freq='3D')
# 右闭左开
pd.date_range(start='2021-10-01', end='2021-10-10', freq='3D', closed='right')
# DatetimeIndex(['2021-10-04', '2021-10-07', '2021-10-10'], dtype='datetime64[ns]', freq='3D')
# 左闭右闭 默认
pd.date_range(start='2021-10-01', end='2021-10-10', freq='3D', closed=None)
# DatetimeIndex(['2021-10-01', '2021-10-04', '2021-10-07', '2021-10-10'], dtype='datetime64[ns]', freq='3D')
- 时区
pd.date_range(start='20211001', periods=5, tz='Asia/Tokyo')
'''
DatetimeIndex(['2021-10-01 00:00:00+09:00', '2021-10-02 00:00:00+09:00',
'2021-10-03 00:00:00+09:00', '2021-10-04 00:00:00+09:00',
'2021-10-05 00:00:00+09:00'],
dtype='datetime64[ns, Asia/Tokyo]', freq='D')
'''
三、其他
- 时间范围
pd.Timestamp.min, pd.Timestamp.max
'''
(Timestamp('1677-09-21 00:12:43.145225'),
Timestamp('2262-04-11 23:47:16.854775807'))
'''
date_range
可能超出时间范围,超出的时间要用 pd.period_range
进行序列构造。
pd.date_range('1510-10-01', periods=10, freq='D')
# OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1510-10-01 00:00:00
pd.period_range('1510-10-01', periods=10, freq='D')
'''
PeriodIndex(['1510-10-01', '1510-10-02', '1510-10-03', '1510-10-04',
'1510-10-05', '1510-10-06', '1510-10-07', '1510-10-08',
'1510-10-09', '1510-10-10'],
dtype='period[D]', freq='D')
'''
- 时间类型转化
# 尝试自动识别时间
pd.to_datetime(s, infer_datetime_format=True)
参考链接:pandas中时间序列——date_range函数
参考链接:pandas 时间序列之pd.date_range()
参考链接:Anchored offsets
参考链接:pandas.date_range