pandas之时间序列(data_range)、重采样(resample)、重组时间序列(PeriodIndex)

1、data_range生成时间范围

a) pd.date_range(start=None, end=None, periods=None, freq='D')
    start和end以及freq配合能够生成start和end范围内以频率freq的一组时间索引
    start和periods以及freq配合能够生成从start开始的频率为freq的periods个时间索引
  
    freq可选择:
      

  b)DataFrame中使用时间序列   

    index=pd.date_range("20170101",periods=10) #生成时间序列

    df = pd.DataFrame(np.random.rand(10),index=index) #将时间序列指定为index

 2、重采样

重采样:指的是将时间序列从一个频率转化为另一个频率进行处理的过程,将高频率数据转化为低频率数据为降采样,低频率转化为高频率为升采样
pandas提供了一个resample的方法来帮助我们实现频率转化

使用案例:
  

 3、重组时间序列:主要将数据中的分离的时间字段,重组为时间序列,并指定为index

#把分开的时间字符串通过periodIndex的方法转化为pandas的时间类型
period = pd.PeriodIndex(year=df["year"],month=df["month"],day=df["day"],hour=df["hour"],freq="H")
print(period)
df["datetime"] = period

#把datetime 设置为索引
df.set_index("datetime",inplace=True)

猜你喜欢

转载自www.cnblogs.com/ywjfx/p/10842614.html