pandas的resample重采樣的使用
Pandas中的resample,重新采樣,是對原樣本重新處理的一個方法,是一個對常規(guī)時間序列數(shù)據(jù)重新采樣和頻率轉(zhuǎn)換的便捷的方法。
降采樣:高頻數(shù)據(jù)到低頻數(shù)據(jù)
升采樣:低頻數(shù)據(jù)到高頻數(shù)據(jù)
主要函數(shù):resample()(pandas對象都會有這個方法)
resample方法的參數(shù)
| 參數(shù) | 說明 |
|---|---|
| freq | 表示重采樣頻率,例如‘M'、‘5min',Second(15) |
| how='mean' | 用于產(chǎn)生聚合值的函數(shù)名或數(shù)組函數(shù),例如‘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)用于將聚合標簽調(diào)早1秒 |
| limit=None | 在向前或向后填充時,允許填充的最大時期數(shù) |
| kind = None | 聚合到時期(‘period')或時間戳(‘timestamp'),默認聚合到時間序列的索引類型 |
| convention = None | 當重采樣時期時,將低頻率轉(zhuǎn)換到高頻率所采用的約定(start或end)。默認‘end' |
首先創(chuàng)建一個Series,采樣頻率為一分鐘。
>>> 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
降低采樣頻率為三分鐘
>>> 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中值的用作標簽。
>>> 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
降低采樣頻率為三分鐘,但是關(guān)閉right區(qū)間。
>>> 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秒
>>> 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值。
>>> 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值。
>>> 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運行一個自定義函數(shù)
>>> 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
到此這篇關(guān)于pandas的resample重采樣的使用的文章就介紹到這了,更多相關(guān)pandas resample重采樣內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python讀取txt文件數(shù)據(jù)的方法(用于接口自動化參數(shù)化數(shù)據(jù))
這篇文章主要介紹了Python讀取txt文件數(shù)據(jù)的方法(用于接口自動化參數(shù)化數(shù)據(jù)),需要的朋友可以參考下2018-06-06
Python調(diào)用百度AI實現(xiàn)人像分割詳解
本文主要介紹了如何通過Python調(diào)用百度AI從而實現(xiàn)人像的分割與合成,文中的示例代碼對我們的工作或?qū)W習有一定的幫助,需要的朋友可以參考一下2021-12-12
Linux下升級安裝python3.8并配置pip及yum的教程
這篇文章主要介紹了Linux下升級安裝python3.8并配置pip及yum的教程,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01

