pd.date_range(
start=None,#开始时间
end=None,#截止时间
periods=None,#总长度
freq=None,#时间间隔
tz=None,#时区
normalize=False,#是否标准化到midnight
name=None,#date名称
closed=None,#首尾是否在内
**kwargs,
)
返回pandas.core.indexes.datetimes.DatetimeIndex
pd.date_range('20201212', periods=10, freq='5H')
pd.date_range('20201111', periods=10, freq='5M')
pd.date_range('20201111', periods=10, freq='5D')
freq可以传入所有的Offset Aliases,具体可查看
https://www.tutorialspoint.com/python_pandas/python_pandas_date_functionality.htm
start, end,periods, freq可以互相搭配产生需要的时间序列。
pd.date_range('20120101', freq='B', periods=10)
DatetimeIndex([‘2012-01-02’, ‘2012-01-03’, ‘2012-01-04’, ‘2012-01-05’,
‘2012-01-06’, ‘2012-01-09’, ‘2012-01-10’, ‘2012-01-11’,
‘2012-01-12’, ‘2012-01-13’],
dtype=‘datetime64[ns]’, freq=‘B’)
pd.bdate_range('20120101', periods=10)
DatetimeIndex([‘2012-01-02’, ‘2012-01-03’, ‘2012-01-04’, ‘2012-01-05’,
‘2012-01-06’, ‘2012-01-09’, ‘2012-01-10’, ‘2012-01-11’,
‘2012-01-12’, ‘2012-01-13’],
dtype=‘datetime64[ns]’, freq=‘B’)
pd.date_range(start='20200407', periods=30, freq=pd.offsets.BDay())
等价于freq=’B’
pd.bdate_range(
start=None,
end=None,
periods=None,
freq='B',#默认为business day
tz=None,
normalize=True,
name=None,
weekmask=None,#str or None, default None'Mon Tue Wed Thu Fri'.
holidays=None,#list-like or None, default None
closed=None,
**kwargs,
)
pd.date_range('1510-01-01', periods=10, freq='D')
OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1510-01-01 00:00:00
pd.Timestamp.min, pd.Timestamp.max
(Timestamp(‘1677-09-21 00:12:43.145225’),
Timestamp(‘2262-04-11 23:47:16.854775807’))
对于超出范围的时间要用pd.period_range()
span = pd.period_range('1215-01-01', '1381-01-01', freq='D')
span
PeriodIndex(['1215-01-01', '1215-01-02', '1215-01-03', '1215-01-04', '1215-01-05', '1215-01-06', '1215-01-07', '1215-01-08', '1215-01-09', '1215-01-10', ... '1380-12-23', '1380-12-24', '1380-12-25', '1380-12-26', '1380-12-27', '1380-12-28', '1380-12-29', '1380-12-30', '1380-12-31', '1381-01-01'], dtype='period[D]', length=60632, freq='D')
如果需要处理的时间序列没有规律,则需要用pd.Period()
import pandas as pd
s = pd.Series([20121231, 20141130, 99991231])
def conv(x):
... return pd.Period(year=x//10000, month=x//100%100, day=x%100, freq='D')
...
s.apply(conv)
0 2012-12-31
1 2014-11-30
2 9999-12-31
dtype: period[D]