DatetimeIndex, TimeSeries ,TimeSeries
1、DatetimeIndex
是pandas库的时间索引
import pandas as pd
import numpy as np
from datetime import datetime
rng = pd.DatetimeIndex(['12/1/2019','12/2/2019','12/3/2019'])
print(rng,type(rng))
------------------------结果-------------------
DatetimeIndex(['2019-12-01', '2019-12-02', '2019-12-03'], dtype='datetime64[ns]', freq=None)
2、TimeSeries
以DatetimeIndex为索引的Series,称为时间序列
import pandas as pd
import numpy as np
from datetime import datetime
st = pd.Series(np.random.rand(len(rng)),index=rng)
print(st,type(st))
------------------------结果--------------------
2019-12-01 0.539328
2019-12-02 0.093855
2019-12-03 0.344785
dtype: float64
生成方式:1.start+end创建 2.start/end +periods 创建,频率默认为天
import pandas as pd
import numpy as np
from datetime import datetime
# date_range 生成日期范围
# 两种生成方式,1,start+end 2,start/end +periods ,频率默认为天
rng1 = pd.date_range('1/1/2019','1/10/2019',normalize=True)
rng2 = pd.date_range('1/1/2019 10:00:00','1/10/2019',normalize=False)
print(rng1)
print(rng2)
rng3 = pd.date_range('1/1/2019',periods=10,normalize=True)
print(rng3)
print('-------------')
# close指定左开或者又开
print(pd.date_range('1/1/2019','1/10/2019',closed='right'))
print(pd.date_range('1/1/2019','1/10/2019',closed='left'))
# bdate_range默认频率为工作日
print(pd.bdate_range('1/1/2019','1/10/2019'))
-----------------------------结果------------------------
DatetimeIndex(['2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04',
'2019-01-05', '2019-01-06', '2019-01-07', '2019-01-08',
'2019-01-09', '2019-01-10'],
dtype='datetime64[ns]', freq='D')
DatetimeIndex(['2019-01-01 10:00:00', '2019-01-02 10:00:00',
'2019-01-03 10:00:00', '2019-01-04 10:00:00',
'2019-01-05 10:00:00', '2019-01-06 10:00:00',
'2019-01-07 10:00:00', '2019-01-08 10:00:00',
'2019-01-09 10:00:00'],
dtype='datetime64[ns]', freq='D')
DatetimeIndex(['2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04',
'2019-01-05', '2019-01-06', '2019-01-07', '2019-01-08',
'2019-01-09', '2019-01-10'],
dtype='datetime64[ns]', freq='D')
-------------
DatetimeIndex(['2019-01-02', '2019-01-03', '2019-01-04', '2019-01-05',
'2019-01-06', '2019-01-07', '2019-01-08', '2019-01-09',
'2019-01-10'],
dtype='datetime64[ns]', freq='D')
DatetimeIndex(['2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04',
'2019-01-05', '2019-01-06', '2019-01-07', '2019-01-08',
'2019-01-09'],
dtype='datetime64[ns]', freq='D')
DatetimeIndex(['2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04',
'2019-01-07', '2019-01-08', '2019-01-09', '2019-01-10'],
dtype='datetime64[ns]', freq='B')
3、一个时间构造器
import pandas as pd
import numpy as np
from datetime import datetime
# pd.period()创建时期,生成一个默认以月为频率的时间构造器
p = pd.Period('2019',freq = 'M')
print(p)
print(p +1)
prng = pd.period_range('1/1/2019','1/1/2020')
print(prng)
# freq可以指定为其他,H为小时
p = pd.Period('2019',freq = 'H')
print(p)
print(p +1)
print(p -1)
----------------------结果---------------------
2019-01
2019-02
PeriodIndex(['2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04',
'2019-01-05', '2019-01-06', '2019-01-07', '2019-01-08',
'2019-01-09', '2019-01-10',
...
'2019-12-23', '2019-12-24', '2019-12-25', '2019-12-26',
'2019-12-27', '2019-12-28', '2019-12-29', '2019-12-30',
'2019-12-31', '2020-01-01'],
dtype='period[D]', length=366, freq='D')
2019-01-01 00:00
2019-01-01 01:00
2018-12-31 23:00