zoukankan      html  css  js  c++  java
  • pandas 时间序列resample


    resample与groupby的区别:
    resample:在给定的时间单位内重取样
    groupby:对给定的数据条目进行统计

    函数原型:
    DataFrame.resample(rule, how=None, axis=0, fill_method=None, closed=None, label=None, convention='start', kind=None, loffset=None, limit=None, base=0)
    其中,参数how已经废弃了


    下面开始练习

    import numpy as np
    import pandas as pd

     
    Start by creating a series with 9 one minute timestamps.

    index = pd.date_range('1/1/2000', periods=9, freq='T')
    series = pd.Series(range(9), index=index)

     
    Downsample the series into 3 minute bins and sum the values of the timestamps falling into a bin.

    series.resample('3T').sum()

     
    To include this value close the right side of the bin interval as illustrated in the example below this one.

    series.resample('3T', label='right').sum()


    Downsample the series into 3 minute bins as above, but close the right side of the bin interval.

    series.resample('3T', label='right', closed='right').sum()


    Upsample the series into 30 second bins.

    series.resample('30S').asfreq()

     
    Upsample the series into 30 second bins and fill the NaN values using the pad method.

    series.resample('30S').pad()

     
    Upsample the series into 30 second bins and fill the NaN values using the bfill method.

    series.resample('30S').bfill()

     
    Pass a custom function via apply

    def custom_resampler(array_like):
        return np.sum(array_like)+5
    
    series.resample('3T').apply(custom_resampler)

     
    附:常见时间频率
    A year
    M month
    W week
    D day
    H hour
    T minute
    S second


  • 相关阅读:
    《构建之法》阅读报告
    教务管理系统类图及数据库E/R图
    设计模式:抽象工厂
    结对项目:四则运算程序测试
    Leetcode笔记之57和为s的连续正数序列
    Leetcode笔记之1103分糖果 II
    Leetcode笔记之199二叉树的右视图
    每日Scrum(9)
    每日Scrum(7)
    每日Scrum(6)
  • 原文地址:https://www.cnblogs.com/hhh5460/p/5596340.html
Copyright © 2011-2022 走看看