python基础-pandas生成时间序列

python基础-pandas生成时间序列

pandas中的date_range()函数用来生成一个日期序列,在需要构造一个日期序列的时候非常方便。

代码

import pandas as pd
import numpy as np
import datetime as dt
#创建2016年7越1号开始,以天为单位,十天的时间序列,指定频率(D\M\H),及倍数,或写起止日
rng = pd.date_range('2016/07/01',periods=10,freq ='3D')
print(rng)
rng = pd.date_range('2016/07/01',periods=10,freq ='M')
print(rng)
#做索引,根据时间做索引
time = pd.Series(np.random.randn(20),
                 index=pd.date_range(dt.datetime (2016,1,1),periods=20))
print(time)
print(time['2016-01-01':'2016-01-04'])
#时间过滤
time.truncate(before='2016-1-10')
time.truncate(after='2016-1-10')
#时间戳
pd.Timestamp('2016-07-01 00:00:00')
#时间区间
print(pd.Period('2016-01'))
print(pd.Period('2016','M'))
print(pd.Period('2016-01','D'))
#时间加减
print(pd.Timedelta('1 day'))
print(pd.Period('2016-01-01 10:10')+pd.Timedelta('1 day'))
p1=pd.period_range('2016-01-01 10:10',freq ='1D1H',periods=10)
print(p1)
print(pd.Period('2016-01-01 10:10')+pd.Timedelta('1 day'))

总结

python基础-pandas生成时间序列_第1张图片

你可能感兴趣的:(python,数据挖掘,数据分析)