"""pandas案例中的一行代碼"""
crime.resample('10AS').sum()
Pandas中的resample,重采樣,是對原樣本重新處理的一個方法,是一個對常規時間序列資料重新采樣和頻率轉換的便捷的方法,正是因為這行代碼中的'10AS'使我萌生了想要弄懂這個函式的想法!
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)
| 引數 | 說明 |
|---|---|
| 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’ |
2 采樣頻率
這個引數是目前使用頻率最高的,所以整理下來供查閱,
example:
index = pd.date_range('1/1/2000', periods=9, freq='T')
series = pd.Series(range(9), index=index)
series
#Output
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
rule 引數如下表 :
| 引數 | 含義 |
|---|---|
| B | business day |
| C | custom business day (experimental) |
| D | calendar day |
| W | weekly |
| M | month end |
| BM | business month end |
| CBM | custom business month end |
| MS | month start |
| BMS | business month start |
| CBMS | custom business month start |
| Q | quarter end |
| BQ | business quarter end |
| QZ | quarter start |
| BS | business quarter start |
| A,Y | year end frequency |
| BA,BY | business year end |
| AS,YS | year start |
| BAS,BYS | business year start |
| BH | business hour |
| H | hourly |
| T,min | minutely |
| S | secondly |
| L | milliseconds |
| M,ms | microseconds |
| N | nanoseconds |
相關鏈接:
Pandas中resample方法詳解_python_腳本之家 (jb51.net)
pandas.DataFrame.resample — pandas 1.3.0 documentation (pydata.org)
pandas resample重采樣頻率介紹 、附案例_code_zbw-CSDN博客
python - What values are valid in Pandas ‘Freq’ tags? - Stack Overflow
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/289294.html
標籤:python
