Pandas中resample方法詳解

Pandas中的resample,重新采樣,是對原樣本重新處理的一個方法,是一個對常規時間序列數據重新采樣和頻率轉換的便捷的方法。

方法的格式是:文章源自四五設計網-http://www.wasochina.com/39152.html

1
DataFrame.resample(rule, how=None, axis=0, fill_method=None, closed=None, label=None, convention='start',kind=None, loffset=None, limit=None, base=0)

參數詳解是:文章源自四五設計網-http://www.wasochina.com/39152.html

 文章源自四五設計網-http://www.wasochina.com/39152.html

參數說明
freq表示重采樣頻率,例如‘M'、‘5min',Second(15)
how='mean'用于產生聚合值的函數名或數組函數,例如‘mean'、‘ohlc'、np.max等,默認是‘mean',其他常用的值由:‘first'、‘last'、‘median'、‘max'、‘min'
axis=0默認是縱軸,橫軸設置axis=1
fill_method = None升采樣時如何插值,比如‘ffill'、‘bfill'等
closed = ‘right'在降采樣時,各時間段的哪一段是閉合的,‘right'或‘left',默認‘right'
label= ‘right'在降采樣時,如何設置聚合值的標簽,例如,9:30-9:35會被標記成9:30還是9:35,默認9:35
loffset = None面元標簽的時間校正值,比如‘-1s'或Second(-1)用于將聚合標簽調早1秒
limit=None在向前或向后填充時,允許填充的最大時期數
kind = None聚合到時期(‘period')或時間戳(‘timestamp'),默認聚合到時間序列的索引類型
convention = None當重采樣時期時,將低頻率轉換到高頻率所采用的約定(start或end)。默認‘end'

 文章源自四五設計網-http://www.wasochina.com/39152.html

首先創建一個Series,采樣頻率為一分鐘。文章源自四五設計網-http://www.wasochina.com/39152.html

1
2
3
4
5
6
7
8
9
10
11
12
13
>>> index = pd.date_range('1/1/2000', periods=9, freq='T')
>>> series = pd.Series(range(9), index=index)
>>> series
2000-01-01 00:00:00? 0
2000-01-01 00:01:00? 1
2000-01-01 00:02:00? 2
2000-01-01 00:03:00? 3
2000-01-01 00:04:00? 4
2000-01-01 00:05:00? 5
2000-01-01 00:06:00? 6
2000-01-01 00:07:00? 7
2000-01-01 00:08:00? 8
Freq: T, dtype: int64

降低采樣頻率為三分鐘文章源自四五設計網-http://www.wasochina.com/39152.html

1
2
3
4
5
>>> series.resample('3T').sum()
2000-01-01 00:00:00?? 3
2000-01-01 00:03:00? 12
2000-01-01 00:06:00? 21
Freq: 3T, dtype: int64

降低采樣頻率為三分鐘,但是每個標簽使用right來代替left。請注意,bucket中值的用作標簽。文章源自四五設計網-http://www.wasochina.com/39152.html

1
2
3
4
5
>>> series.resample('3T', label='right').sum()
2000-01-01 00:03:00?? 3
2000-01-01 00:06:00? 12
2000-01-01 00:09:00? 21
Freq: 3T, dtype: int64

降低采樣頻率為三分鐘,但是關閉right區間。文章源自四五設計網-http://www.wasochina.com/39152.html

1
2
3
4
5
6
>>> series.resample('3T', label='right', closed='right').sum()
2000-01-01 00:00:00?? 0
2000-01-01 00:03:00?? 6
2000-01-01 00:06:00? 15
2000-01-01 00:09:00? 15
Freq: 3T, dtype: int64

增加采樣頻率到30秒文章源自四五設計網-http://www.wasochina.com/39152.html

1
2
3
4
5
6
7
>>> series.resample('30S').asfreq()[0:5] #select first 5 rows
2000-01-01 00:00:00?? 0
2000-01-01 00:00:30? NaN
2000-01-01 00:01:00?? 1
2000-01-01 00:01:30? NaN
2000-01-01 00:02:00?? 2
Freq: 30S, dtype: float64

增加采樣頻率到30S,使用pad方法填充nan值。文章源自四五設計網-http://www.wasochina.com/39152.html

1
2
3
4
5
6
7
>>> series.resample('30S').pad()[0:5]
2000-01-01 00:00:00? 0
2000-01-01 00:00:30? 0
2000-01-01 00:01:00? 1
2000-01-01 00:01:30? 1
2000-01-01 00:02:00? 2
Freq: 30S, dtype: int64

增加采樣頻率到30S,使用bfill方法填充nan值。

1
2
3
4
5
6
7
>>> series.resample('30S').bfill()[0:5]
2000-01-01 00:00:00? 0
2000-01-01 00:00:30? 1
2000-01-01 00:01:00? 1
2000-01-01 00:01:30? 2
2000-01-01 00:02:00? 2
Freq: 30S, dtype: int64

通過apply運行一個自定義函數

1
2
3
4
5
6
7
>>> def custom_resampler(array_like):
...?? return np.sum(array_like)+5
>>> series.resample('3T').apply(custom_resampler)
2000-01-01 00:00:00?? 8
2000-01-01 00:03:00? 17
2000-01-01 00:06:00? 26
Freq: 3T, dtype: int64

以上就是本文的全部內容,希望對大家的學習有所幫助。

繼續閱讀
我的微信
微信掃一掃
weinxin
我的微信
惠生活福利社
微信掃一掃
weinxin
我的公眾號
 
  • 本文由 四五設計網小助手 發表于 2024年1月25日11:09:06
  • 轉載請務必保留本文鏈接:http://www.wasochina.com/39152.html

發表評論

匿名網友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

拖動滑塊以完成驗證