SpaceMonkey
SpaceMonkey

Reputation: 4365

Returns NaN values when calculating exponentially weighted moving average -Pandas

The problem is simple:

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)

This is how the data looks now:

enter image description here

Now if I try to calculate exponentially weighted moving average while regularising the time:

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

I get:

enter image description here

Why am I getting NaN?

Upvotes: 2

Views: 619

Answers (1)

ℕʘʘḆḽḘ
ℕʘʘḆḽḘ

Reputation: 19375

try this little monkey

 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

Upvotes: 1

Related Questions