在计算指数加权移动平均值时,返回NaN值-Pandas

时间:2017-07-30 15:40:31

标签: python 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

为什么我会得到NaN?

1 个答案:

答案 0 :(得分: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