返回固定频率下的datetime
1 使用方法
pandas.date_range(start=None, end=None, periods=None, freq=None, tz=None, normalize=False, name=None, inclusive='both', *, unit=None, **kwargs)
2 基本参数
start、end、periods至少需要两个
start | 生成日期的左边界 | ||||||||||||||||||||||||||
end | 生成日期的右边界 | ||||||||||||||||||||||||||
periods | 要生成的周期数 | ||||||||||||||||||||||||||
freq | 间隔频率
| ||||||||||||||||||||||||||
normalize | 布尔值,默认为 False 在生成日期范围之前,将开始/结束日期归一化为午夜 | ||||||||||||||||||||||||||
name | 生成的 DatetimeIndex 的名称 | ||||||||||||||||||||||||||
inclusive | {"both", "neither", "left", "right"} 包括边界;是否将每个边界设置为闭区间或开区间 |
3 举例说明
from datetime import datetime
import pandas as pdpd.date_range(start=datetime(2023,3,1),end=datetime(2023,3,5))
'''
DatetimeIndex(['2023-03-01', '2023-03-02', '2023-03-03', '2023-03-04','2023-03-05'],dtype='datetime64[ns]', freq='D')
'''
pd.date_range(start=datetime(2023,3,1),end=datetime(2023,3,5),periods=8)
'''
DatetimeIndex([ '2023-03-01 00:00:00','2023-03-01 13:42:51.428571428','2023-03-02 03:25:42.857142857','2023-03-02 17:08:34.285714285','2023-03-03 06:51:25.714285714','2023-03-03 20:34:17.142857142','2023-03-04 10:17:08.571428571','2023-03-05 00:00:00'],dtype='datetime64[ns]', freq=None)
'''
pd.date_range(start=datetime(2023,3,1),end=datetime(2023,3,5),freq='30min')
'''
DatetimeIndex(['2023-03-01 00:00:00', '2023-03-01 00:30:00','2023-03-01 01:00:00', '2023-03-01 01:30:00','2023-03-01 02:00:00', '2023-03-01 02:30:00','2023-03-01 03:00:00', '2023-03-01 03:30:00','2023-03-01 04:00:00', '2023-03-01 04:30:00',...'2023-03-04 19:30:00', '2023-03-04 20:00:00','2023-03-04 20:30:00', '2023-03-04 21:00:00','2023-03-04 21:30:00', '2023-03-04 22:00:00','2023-03-04 22:30:00', '2023-03-04 23:00:00','2023-03-04 23:30:00', '2023-03-05 00:00:00'],dtype='datetime64[ns]', length=193, freq='30T')
'''