Pandas —— resample()重采樣和asfreq()頻度轉(zhuǎn)換方式
resample()
resample()進(jìn)行重采樣。
重采樣(Resampling)指的是把時(shí)間序列的頻度變?yōu)榱硪粋€(gè)頻度的過程。把高頻度的數(shù)據(jù)變?yōu)榈皖l度叫做降采樣(downsampling),把低頻度變?yōu)楦哳l度叫做增采樣(upsampling)。
降采樣
考慮因素:
各區(qū)間哪邊是閉合的(參數(shù):closed)
如何標(biāo)記各聚合面元,用區(qū)間的開頭還是末尾(參數(shù):label)
In [232]: ts_index = pd.date_range('2018-08-03',periods =12,freq = 'T') In [233]: ts = pd.Series(np.arange(12),index = ts_index) In [234]: ts Out[234]: 2018-08-03 00:00:00 0 2018-08-03 00:01:00 1 2018-08-03 00:02:00 2 2018-08-03 00:03:00 3 2018-08-03 00:04:00 4 2018-08-03 00:05:00 5 2018-08-03 00:06:00 6 2018-08-03 00:07:00 7 2018-08-03 00:08:00 8 2018-08-03 00:09:00 9 2018-08-03 00:10:00 10 2018-08-03 00:11:00 11 Freq: T, dtype: int32
默認(rèn)使用左標(biāo)簽(label=‘left'),左閉合(closed='left')
此時(shí)第一個(gè)區(qū)間為:2018-08-03 00:00:00~2018-08-03 00:04:59,故sum為10,label為:2018-08-03 00:00:00
In [235]: ts.resample('5min').sum() Out[235]: 2018-08-03 00:00:00 10 2018-08-03 00:05:00 35 2018-08-03 00:10:00 21 Freq: 5T, dtype: int32
可以指定為右閉合(closed='right'),默認(rèn)使用左標(biāo)簽(label=‘left')
此時(shí)第一個(gè)區(qū)間為:2018-08-02 23:55:01~2018-08-03 00:00:00,故sum為0,label為:2018-08-02 23:55:00
In [236]: ts.resample('5min',closed='right').sum() Out[236]: 2018-08-02 23:55:00 0 2018-08-03 00:00:00 15 2018-08-03 00:05:00 40 2018-08-03 00:10:00 11 Freq: 5T, dtype: int32
可以指定為右閉合(closed='right'),右標(biāo)簽(label=‘right')
此時(shí)第一個(gè)區(qū)間為:2018-08-02 23:55:01~2018-08-03 00:00:00,故sum為0,label為:2018-08-03 00:00:00
In [237]: ts.resample('5min',closed='right',label='right').sum() Out[237]: 2018-08-03 00:00:00 0 2018-08-03 00:05:00 15 2018-08-03 00:10:00 40 2018-08-03 00:15:00 11 Freq: 5T, dtype: int32
升采樣
考慮因素:
沒有聚合,但是需要填充
In [244]: frame = pd.DataFrame(np.random.randn(2, 4), ...: index=pd.date_range('1/1/2000', periods=2, ...: freq='W-WED'), # freq='W-WED'表示按周 ...: columns=['Colorado', 'Texas', 'New York', 'Ohio']) In [245]: frame Out[245]: Colorado Texas New York Ohio 2000-01-05 1.201713 0.029819 -1.366082 -1.325252 2000-01-12 -0.711291 -1.070133 1.469272 0.809806
當(dāng)我們對(duì)這個(gè)數(shù)據(jù)進(jìn)行聚合的的時(shí)候,每個(gè)組只有一個(gè)值,以及gap(間隔)之間的缺失值。在不使用任何聚合函數(shù)的情況下,
我們使用asfreq方法將其轉(zhuǎn)換為高頻度:
In [246]: df_daily = frame.resample('D').asfreq() In [247]: df_daily Out[247]: Colorado Texas New York Ohio 2000-01-05 1.201713 0.029819 -1.366082 -1.325252 2000-01-06 NaN NaN NaN NaN 2000-01-07 NaN NaN NaN NaN 2000-01-08 NaN NaN NaN NaN 2000-01-09 NaN NaN NaN NaN 2000-01-10 NaN NaN NaN NaN 2000-01-11 NaN NaN NaN NaN 2000-01-12 -0.711291 -1.070133 1.469272 0.809806
使用ffill()進(jìn)行填充
In [248]: frame.resample('D').ffill() Out[248]: Colorado Texas New York Ohio 2000-01-05 1.201713 0.029819 -1.366082 -1.325252 2000-01-06 1.201713 0.029819 -1.366082 -1.325252 2000-01-07 1.201713 0.029819 -1.366082 -1.325252 2000-01-08 1.201713 0.029819 -1.366082 -1.325252 2000-01-09 1.201713 0.029819 -1.366082 -1.325252 2000-01-10 1.201713 0.029819 -1.366082 -1.325252 2000-01-11 1.201713 0.029819 -1.366082 -1.325252 2000-01-12 -0.711291 -1.070133 1.469272 0.809806 In [249]: frame.resample('D').ffill(limit=2) Out[249]: Colorado Texas New York Ohio 2000-01-05 1.201713 0.029819 -1.366082 -1.325252 2000-01-06 1.201713 0.029819 -1.366082 -1.325252 2000-01-07 1.201713 0.029819 -1.366082 -1.325252 2000-01-08 NaN NaN NaN NaN 2000-01-09 NaN NaN NaN NaN 2000-01-10 NaN NaN NaN NaN 2000-01-11 NaN NaN NaN NaN 2000-01-12 -0.711291 -1.070133 1.469272 0.809806
新的日期索引沒必要跟舊的重疊
In [250]: frame.resample('W-THU').ffill() Out[250]: Colorado Texas New York Ohio 2000-01-06 1.201713 0.029819 -1.366082 -1.325252 2000-01-13 -0.711291 -1.070133 1.469272 0.809806
分組重采樣
In [279]: times = pd.date_range('2018-08-3 00:00', freq='1min', periods=10) In [280]: df2 = pd.DataFrame({'time': times.repeat(3), ...: 'key': np.tile(['a', 'b', 'c'], 10), ...: 'value': np.arange(30)}) In [281]: df2[:5] Out[281]: key time value 0 a 2018-08-03 00:00:00 0 1 b 2018-08-03 00:00:00 1 2 c 2018-08-03 00:00:00 2 3 a 2018-08-03 00:01:00 3 4 b 2018-08-03 00:01:00 4 In [282]: df2.groupby(['key',pd.Grouper(key='time',freq='5min')]).sum() Out[282]: value key time a 2018-08-03 00:00:00 30 2018-08-03 00:05:00 105 b 2018-08-03 00:00:00 35 2018-08-03 00:05:00 110 c 2018-08-03 00:00:00 40 2018-08-03 00:05:00 115
asfreq()
asfreq()進(jìn)行頻度轉(zhuǎn)換。
>>> index = pd.date_range('1/1/2000', periods=4, freq='T') >>> series = pd.Series([0.0, None, 2.0, 3.0], index=index) >>> df = pd.DataFrame({'s':series}) >>> df s 2000-01-01 00:00:00 0.0 2000-01-01 00:01:00 NaN 2000-01-01 00:02:00 2.0 2000-01-01 00:03:00 3.0
將頻度轉(zhuǎn)換為30s
>>> df.asfreq(freq='30S') s 2000-01-01 00:00:00 0.0 2000-01-01 00:00:30 NaN 2000-01-01 00:01:00 NaN 2000-01-01 00:01:30 NaN 2000-01-01 00:02:00 2.0 2000-01-01 00:02:30 NaN 2000-01-01 00:03:00 3.0
將頻度轉(zhuǎn)換為2min,不會(huì)進(jìn)行重采樣(與resample的不同之處)
>>> df.asfreq(freq='2min') s 2000-01-01 00:00:00 0.0 2000-01-01 00:02:00 2.0
使用bfill()進(jìn)行填充
>>> df.asfreq(freq='30S').bfill() s 2000-01-01 00:00:00 0.0 2000-01-01 00:00:30 NaN 2000-01-01 00:01:00 NaN 2000-01-01 00:01:30 2.0 2000-01-01 00:02:00 2.0 2000-01-01 00:02:30 3.0 2000-01-01 00:03:00 3.0
以上這篇Pandas —— resample()重采樣和asfreq()頻度轉(zhuǎn)換方式就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
numpy數(shù)組做圖片拼接的實(shí)現(xiàn)(concatenate、vstack、hstack)
這篇文章主要介紹了numpy數(shù)組做圖片拼接的實(shí)現(xiàn)(concatenate、vstack、hstack),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11python批量獲取html內(nèi)body內(nèi)容的實(shí)例
今天小編就為大家分享一篇python批量獲取html內(nèi)body內(nèi)容的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01python Bamboolib庫(kù)加速Pandas數(shù)據(jù)分析過程詳解
這篇文章主要介紹了python Bamboolib庫(kù)加速Pandas數(shù)據(jù)分析過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01linux平臺(tái)使用Python制作BT種子并獲取BT種子信息的方法
這篇文章主要介紹了linux平臺(tái)使用Python制作BT種子并獲取BT種子信息的方法,結(jié)合實(shí)例形式詳細(xì)分析了Python BT模塊的安裝及針對(duì)BT種子文件的相關(guān)操作技巧,需要的朋友可以參考下2017-01-01python從Oracle讀取數(shù)據(jù)生成圖表
這篇文章主要介紹了python如何從Oracle讀取數(shù)據(jù)生成圖表,幫助大家更好的利用python處理數(shù)據(jù),感興趣的朋友可以了解下2020-10-10Python實(shí)現(xiàn)批量把SVG格式轉(zhuǎn)成png、pdf格式的代碼分享
這篇文章主要介紹了Python實(shí)現(xiàn)批量把SVG格式轉(zhuǎn)成png、pdf格式的代碼分享,本文代碼需要引用一個(gè)第三方模塊cairosvg,需要的朋友可以參考下2014-08-08Python中微服務(wù)架構(gòu)的設(shè)計(jì)與實(shí)現(xiàn)詳解
在當(dāng)今軟件開發(fā)領(lǐng)域中,微服務(wù)架構(gòu)已經(jīng)成為了一種流行的設(shè)計(jì)范式,這篇文章主要為大家介紹了如何使用Python語(yǔ)言來設(shè)計(jì)和實(shí)現(xiàn)微服務(wù)架構(gòu),需要的可以參考一下2024-04-04