週期由高頻率轉向低頻率稱爲降採樣:例如5分鐘股票交易數據轉換爲日交易數據dom
相反,週期也能夠由低頻轉向高頻稱爲升採樣spa
其餘重採樣:例如每週三(W-WED)轉換爲每週五(W-FRI)code
1 import pandas as pd 2 import numpy as np 3 4 # 建立一個時間戳序列 5 s = pd.Series(np.random.randn(5), 6 index=pd.date_range('2016-04-01',periods=5,freq='M')) 7 # 注意它給的起始時間,與輸出的時間對比, 8 # 它給定的頻率爲月份輸出的月份從每一個月的最後一天算起 9 # 輸出 10 2016-04-30 -0.487238 11 2016-05-31 0.376708 12 2016-06-30 -1.830840 13 2016-07-31 -0.426218 14 2016-08-31 1.913151 15 Freq: M, dtype: float64 16 17 # 將時間戳的序列轉換爲時期序列, 18 s.to_period() 19 # 輸出 20 2016-04 -0.487238 21 2016-05 0.376708 22 2016-06 -1.830840 23 2016-07 -0.426218 24 2016-08 1.913151 25 Freq: M, dtype: float64 26 27 # 建立週期頻率爲天的時間序列 28 ts = pd.Series(np.random.randn(5), 29 index=pd.date_range('2016-12-29',periods=5,freq='D')) 30 # 這個時間序列與第5行的不一樣,它的頻率變以天爲單位 31 32 # 當轉換爲時期序列,它的頻率也是默認天爲單位 33 ts.to_period() # 與28行的結果相同 34 35 pts = ts.to_period(freq='M') 36 # 把頻率變爲月時原來的總時間沒變只是頻率變了它將輸出 37 2016-12 -0.525272 38 2016-12 -2.610914 39 2016-12 1.094692 40 2017-01 -1.721324 41 2017-01 0.631946 42 Freq: M, dtype: float64 43 44 # 也能夠再轉爲時間戳序列 45 pts.to_timestamp() 46 # 輸出 47 2016-12-01 0.797379 48 2016-12-01 -0.085046 49 2016-12-01 -0.271226 50 2017-01-01 1.320668 51 2017-01-01 0.168546 52 dtype: float64 53 54 pts.to_timestamp(how='end') # 能夠輸出每個月的最後結束時間 55 # 輸出 56 2016-12-31 23:59:59.999999999 0.797379 57 2016-12-31 23:59:59.999999999 -0.085046 58 2016-12-31 23:59:59.999999999 -0.271226 59 2017-01-31 23:59:59.999999999 1.320668 60 2017-01-31 23:59:59.999999999 0.168546 61 dtype: float64 62 63 # 建立以週期頻率爲分的時間序列 64 ts = pd.Series(np.random.randint(0,50,60), 65 index=pd.date_range('2016-04-25 09:30',periods=60,freq='T')) 66 # 經過降採樣,下降時間頻率 67 ts.resample('5min',how='sum') # how='sum',表示對降採樣的時間段求和 68 # 輸出 它是以時間開始的時候爲準,即時間軸的左端 69 2016-04-25 09:30:00 135 70 2016-04-25 09:35:00 120 71 2016-04-25 09:40:00 138 72 2016-04-25 09:45:00 101 73 ...... 74 75 ts.resample('5min',how='sum',label='right') # 也能夠以末尾時間爲準,即時間軸右端 76 # 輸出 77 2016-04-25 09:35:00 135 78 2016-04-25 09:40:00 120 79 2016-04-25 09:45:00 138 80 2016-04-25 09:50:00 101 81 ...... 82 83 ts.resample('5min',how='ohlc') 84 # 它建立了一個DataFrame,以時間爲行索引,分別以open、high、low、close 85 # 爲列索引,how='ohlc'就是前面每一個列索引的首字母 86 87 ts = pd.Series(np.random.randint(0,50,100), 88 index=pd.date_range('2016-03-01',periods=100,freq='D')) 89 90 ts.groupby(lambda x: x.month).sum() 91 # lambda表達式中的x爲ts,把ts中的月份進行分組並求每個月的總量 92 ts.groupby(ts.index.to_period('M')).sum() # 與上一行效果相同
1 import pandas as pd 2 import numpy as np 3 4 df = pd.DataFrame(np.random.randint(1,50,2), 5 index=pd.date_range('2016-04-22',periods=2,freq='W-FRI')) 6 # 它的週期頻率爲星期五 7 8 df.resample('D',fill_method='ffill') 9 # 用升採樣的方式,將頻率提升的天天, 10 # fill_method='ffill',表示向上填充值,即所要填充的值與上一行相同 11 12 df.resample('D',fill_method='ffill',limit=3) 13 # limit=3表示最後3行被限制不填入值,默認填NaN 14 15 df.resample('W-MON',fill_method='ffill') 16 # 表示以以週一爲頻率從新採樣 17 18 19 df = pd.DataFrame(np.random.randint(2,30,(24,4)), 20 index=pd.period_range('2015-01','2016-12',freq='M'), 21 columns=list('ABCD') 22 23 df.resample('A-DEC',how='sum') # 用年頻率重採樣 24 25 df.resample('A-MAR',how='sum') # 用財年重採樣,每一年的三月分 26 27 pdf = df.resample('A-DEC',how='mean') # 用年頻率重採樣,算出每一年的均值 28 29 pdf.resample('Q-DEC',fill_method='ffill') # 將上一行的值再以季度頻率重採樣