• 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)将时间字符串转为时间序列  

        使用pandas提供的方法把时间字符串转化为时间序列

        df["timeStamp"] = pd.to_datetime(df["timeStamp"],format=""),其中format参数大部分情况下可以不用写

      c)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)
  • 相关阅读:
    .NET Framework类库大概
    CTS、CLS、CIL与CLR
    .NETFramework、CLR、.NET Framework class library、托管代码是什么
    万维网三大核心技术
    Clang、GCC和LLVM是什么
    常见开源许可证、开源协议GPL、BSD、MIT、Mozilla、Apache和LGPL之间区别
    编译原理之变量、名字与标识符
    编译原理之静态策略与动态策略
    Jupyter Notebook打开任意文件
    修改VsCodes的语言为中文
  • 原文地址:https://www.cnblogs.com/ywjfx/p/10842614.html
Copyright © 2020-2023  润新知