Pandas時(shí)間序列:重采樣及頻率轉(zhuǎn)換方式
如下所示:
import pandas as pd import numpy as np
一、介紹
重采樣(resampling)指的是將時(shí)間序列從一個(gè)頻率轉(zhuǎn)換到另一個(gè)頻率的處理過(guò)程;
將高頻率(間隔短)數(shù)據(jù)聚合到低頻率(間隔長(zhǎng))稱為降采樣(downsampling);
將低頻率數(shù)據(jù)轉(zhuǎn)換到高頻率則稱為升采樣(unsampling);
有些采樣即不是降采樣也不是升采樣,例如將W-WED(每周三)轉(zhuǎn)換為W-FRI;
二、resample方法–轉(zhuǎn)換頻率的主力函數(shù)
rng = pd.date_range('1/1/2000',periods=100,freq='D')
ts = pd.Series(np.random.randn(len(rng)),index=rng)
ts.resample('M').mean() # 將100天按月進(jìn)行降采樣(聚合)
2000-01-31 -0.156092 2000-02-29 0.060607 2000-03-31 -0.039608 2000-04-30 -0.154838 Freq: M, dtype: float64
ts.resample('M',kind='period').mean()
2000-01 -0.156092 2000-02 0.060607 2000-03 -0.039608 2000-04 -0.154838 Freq: M, dtype: float64
三、降采樣(聚合)
1.降采樣面元(區(qū)間)默認(rèn)才有左閉右開(kāi)的形式,而且聚合的索引是以左邊界標(biāo)記
rng = pd.date_range('1/1/2000',periods=12,freq='T')
ts = pd.Series(np.arange(12),index=rng)
ts
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 2000-01-01 00:09:00 9 2000-01-01 00:10:00 10 2000-01-01 00:11:00 11 Freq: T, dtype: int32
ts.resample('5min').sum()
2000-01-01 00:00:00 10 2000-01-01 00:05:00 35 2000-01-01 00:10:00 21 Freq: 5T, dtype: int32
2.通過(guò)參數(shù)closed='right'可以實(shí)現(xiàn)左開(kāi)右閉
ts.resample('5min',closed='right').sum()
1999-12-31 23:55:00 0 2000-01-01 00:00:00 15 2000-01-01 00:05:00 40 2000-01-01 00:10:00 11 Freq: 5T, dtype: int32
3.通過(guò)參數(shù)label='right'可以實(shí)現(xiàn)以右邊界為聚合后的標(biāo)簽
ts.resample('5min',closed='right',label='right').sum()
2000-01-01 00:00:00 0 2000-01-01 00:05:00 15 2000-01-01 00:10:00 40 2000-01-01 00:15:00 11 Freq: 5T, dtype: int32
4.通過(guò)參數(shù)loffset可以實(shí)現(xiàn)精準(zhǔn)的調(diào)整標(biāo)簽
ts.resample('5min',closed='right',loffset='-1s').sum()
1999-12-31 23:54:59 0 1999-12-31 23:59:59 15 2000-01-01 00:04:59 40 2000-01-01 00:09:59 11 Freq: 5T, dtype: int32
四、OHLC重采樣
在金融領(lǐng)域常用的聚合方式–OHLC,它會(huì)計(jì)算各個(gè)面元的:第一個(gè)值(開(kāi)盤)、最后一個(gè)值(收盤)、最大值和最小值,并產(chǎn)生一個(gè)DataFrame
print(ts.resample('5min').ohlc())
open high low close
2000-01-01 00:00:00 0 4 0 4
2000-01-01 00:05:00 5 9 5 9
2000-01-01 00:10:00 10 11 10 11
五、通過(guò)groupby進(jìn)行重采樣
rng = pd.date_range('1/1/2000',periods=100,freq='D')
ts = pd.Series(np.arange(100),index=rng)
ts.groupby(lambda x:x.month).mean() # 等價(jià)于 ts.groupby(rng.month).mean()
1 15 2 45 3 75 4 95 dtype: int32
ts.groupby(lambda x:x.weekday).mean() # 按周聚合
0 47.5 1 48.5 2 49.5 3 50.5 4 51.5 5 49.0 6 50.0 dtype: float64
六、升采樣和插值
升采樣是從低頻率到高頻率,這樣會(huì)引入缺失值;
升采樣時(shí)需要決定采樣后結(jié)果中具體那個(gè)值代替原始的值;
當(dāng)決定了替換原始值的值后,中間的值會(huì)按照頻率進(jìn)行添加;
frame = pd.DataFrame(np.random.randn(2,4),
index = pd.date_range('1/1/2000',periods=2,freq='W-WED'),
columns = ['Colorado','Texas','New York','Ohio'])
print(frame)
Colorado Texas New York Ohio
2000-01-05 -0.078765 1.389417 0.732726 0.816723
2000-01-12 -0.663686 0.744384 1.395332 -0.031715
1.升采樣、前向填充
df_daily = frame.resample('D')
print(df_daily.ffill())
Colorado Texas New York Ohio
2000-01-05 -0.078765 1.389417 0.732726 0.816723
2000-01-06 -0.078765 1.389417 0.732726 0.816723
2000-01-07 -0.078765 1.389417 0.732726 0.816723
2000-01-08 -0.078765 1.389417 0.732726 0.816723
2000-01-09 -0.078765 1.389417 0.732726 0.816723
2000-01-10 -0.078765 1.389417 0.732726 0.816723
2000-01-11 -0.078765 1.389417 0.732726 0.816723
2000-01-12 -0.663686 0.744384 1.395332 -0.031715
print(df_daily.ffill(limit=2))
Colorado Texas New York Ohio
2000-01-05 -0.078765 1.389417 0.732726 0.816723
2000-01-06 -0.078765 1.389417 0.732726 0.816723
2000-01-07 -0.078765 1.389417 0.732726 0.816723
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.663686 0.744384 1.395332 -0.031715
2.重采樣后的日期不一定與先前的日期有交集
print(frame)
Colorado Texas New York Ohio
2000-01-05 -0.078765 1.389417 0.732726 0.816723
2000-01-12 -0.663686 0.744384 1.395332 -0.031715
print(frame.resample('W-THU').ffill()) # 重采樣后的結(jié)果開(kāi)始為全NaN,使用ffill會(huì)使用2000-01-05和2000-01-12的值向前填充
Colorado Texas New York Ohio
2000-01-06 -0.078765 1.389417 0.732726 0.816723
2000-01-13 -0.663686 0.744384 1.395332 -0.031715
七、通過(guò)時(shí)期(period)進(jìn)行重采樣
1.將采樣
frame = pd.DataFrame(np.random.randn(24,4),
index = pd.period_range('1-2000','12-2001',freq='M'),
columns = ['Colorado','Texas','New York','Ohio'])
print(frame[:5])
Colorado Texas New York Ohio
2000-01 -1.956495 -0.689508 0.057439 -0.655832
2000-02 -0.491443 -1.731887 1.336801 0.659877
2000-03 -0.139601 -1.310386 -0.299205 1.194269
2000-04 0.431474 -1.312518 1.880223 0.379421
2000-05 -0.674796 0.471018 0.132998 0.509761
annual_frame = frame.resample('A-DEC').mean()
print(annual_frame)
Colorado Texas New York Ohio 2000 -0.332076 -0.762599 0.046917 0.224908 2001 -0.152922 0.168667 -0.326439 -0.052034
2.通過(guò)convention決定在升采樣后,那端來(lái)替換原來(lái)的值
# Q-DEC:以12月做為最后一個(gè)季度的最后一個(gè)月進(jìn)行升采樣.也就是1-3月是1季度,4-6月是2季度,7-9月是3季度,10-12月是4季度
print(annual_frame.resample('Q-DEC').ffill())
Colorado Texas New York Ohio
2000Q1 -0.332076 -0.762599 0.046917 0.224908
2000Q2 -0.332076 -0.762599 0.046917 0.224908
2000Q3 -0.332076 -0.762599 0.046917 0.224908
2000Q4 -0.332076 -0.762599 0.046917 0.224908
2001Q1 -0.152922 0.168667 -0.326439 -0.052034
2001Q2 -0.152922 0.168667 -0.326439 -0.052034
2001Q3 -0.152922 0.168667 -0.326439 -0.052034
2001Q4 -0.152922 0.168667 -0.326439 -0.052034
# 使用2000Q4替換2000、2001Q4替換2001,這兩個(gè)值2000Q4和2001Q4之間就是升采樣新增的值
print(annual_frame.resample('Q-DEC',convention='end').ffill())
Colorado Texas New York Ohio
2000Q4 -0.332076 -0.762599 0.046917 0.224908
2001Q1 -0.332076 -0.762599 0.046917 0.224908
2001Q2 -0.332076 -0.762599 0.046917 0.224908
2001Q3 -0.332076 -0.762599 0.046917 0.224908
2001Q4 -0.152922 0.168667 -0.326439 -0.052034
3.綜合案例解析
Q-MAR:4-6月是1季度,7-9月是2季度,10-12月是3季度,1-3月是4季度;
2000-01到2000-03是2000Q4,2000-04到2000-6是2001Q1,以此類推;
2000轉(zhuǎn)變?yōu)閇2000Q4,2001Q1,2001Q2,2001Q3],2001轉(zhuǎn)變?yōu)閇2001Q4,2002Q1,2002Q2,2002Q3];
convention='end',那么會(huì)使用2001Q3替換原始的2000,2002Q3替換2001,中間的部分自動(dòng)添加;
索引結(jié)果為[2001Q3,2001Q4,2002Q1,2002Q2,2002Q3];
print(annual_frame.resample('Q-MAR',convention='end').ffill())
Colorado Texas New York Ohio
2001Q3 -0.332076 -0.762599 0.046917 0.224908
2001Q4 -0.332076 -0.762599 0.046917 0.224908
2002Q1 -0.332076 -0.762599 0.046917 0.224908
2002Q2 -0.332076 -0.762599 0.046917 0.224908
2002Q3 -0.152922 0.168667 -0.326439 -0.052034
以上這篇Pandas時(shí)間序列:重采樣及頻率轉(zhuǎn)換方式就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python通過(guò)txt文件批量安裝依賴包的實(shí)現(xiàn)步驟
今天小編就為大家分享一篇python通過(guò)txt文件批量安裝依賴包的實(shí)現(xiàn)步驟,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08
在IPython中進(jìn)行Python程序執(zhí)行時(shí)間的測(cè)量方法
今天小編就為大家分享一篇在IPython中進(jìn)行Python程序執(zhí)行時(shí)間的測(cè)量方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
基于Python編寫一個(gè)打印機(jī)批量打印隊(duì)列工具
有時(shí)候我們?cè)谂看蛴∥募臅r(shí)候,總會(huì)遇到電腦上打印機(jī)隊(duì)列打不開(kāi)的情況,為此我們可以利用Python寫一個(gè)打印機(jī)批量打印隊(duì)列,下面小編就來(lái)和大家詳細(xì)講講吧2025-02-02
詳解Python是如何實(shí)現(xiàn)issubclass的
這篇文章主要介紹了詳解Python是如何實(shí)現(xiàn)issubclass的,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Python3如何使用range函數(shù)替代xrange函數(shù)
這篇文章主要介紹了Python3如何使用range函數(shù)替代xrange函數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10

