What is the best way to get exponential moving average of pandas time-series with irregular time intervals?

Viewed 270

I have a pandas series object which is a time-series with irregular time intervals:

import numpy as np
import pandas as pd
size = 10
df = pd.DataFrame(
    {"A": np.random.uniform(size=size),
     "ts": pd.date_range('2019-01-01', periods=size, freq='1min')}
)
df["ts"] += pd.to_timedelta(df["A"] * 60, unit="s")
df = df.set_index("ts")

I would like to calculate exponential moving average for the values but it seems all builtin pandas functions assume regular time values. Does pandas have a good way to deal with this?

I have already tried resampling the series, but that is not ideal because I want the ewma at the irregular points not sampled regular points. Also, I have written a custom streaming object which I can apply to the values but it requires iterating over the series timestamps and values and is not fast enough. Ideally I want a builtin pandas function which can do ewma taking into account the time index.

my approach can be found in this gist: https://gist.github.com/scgalois/c9aff545c1e841b5835330b26b6bc9bb

As you can see that is a lot of code to achieve something simple. I need a proper way to do this with pandas.

0 Answers
Related