2017-07-30 121 views
2

的問題很簡單:返回NaN值在計算指數加權移動平均-Pandas

import random, datetime, pandas as pd 

    rows = 100 

    random_times = pd.Series([datetime.datetime.now().replace(day = 1+ int(i/60/ 24) % 28, hour= int(i/60) % 24, minute=i%60, second=random.randint(0, 59)) for i in range(rows)]) 
    a = pd.DataFrame({'values': [i + random.random()*50 for i in range(10, 10+rows)]}, index=random_times) 

這是該數據現在的樣子:

enter image description here

現在,如果我嘗試成倍計算加權移動平均值,同時調整時間:

a['values'].ewm(span=50, freq='S').mean()# S for seconds 

我得到:

enter image description here

爲什麼我收到喃?

回答

1

試試這個小猴子

a['values'].resample('S').ewm(span=50, freq='S').mean() 

Out[147]: 
2017-07-01 00:00:00  56.684041 
2017-07-01 00:00:01  56.684041 
2017-07-01 00:00:02  56.684041 
2017-07-01 00:00:03  56.684041 
2017-07-01 00:00:04  56.684041 
2017-07-01 00:00:05  56.684041 
+0

有什麼用「頻率=‘S’」對一個已經通過第二採樣數據的意義呢?如果您刪除該參數,則會得到相同的結果。 –

+0

是的刪除。這是你期望的輸出嗎? –

+0

nope,它與ewm(freq =)應該做的不匹配:https://i.imgur.com/HjQX2SI.png 你可以看到結果是不同的 –