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



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM