0

Pandas a has a very efficient method to calculate ewm, I looked at the source code but cannot find the actual method used. I am assuming it is cythonised for speed.

Can someone show the actual method to do it so quickly, since the only way I can think of is it to iterate and calc:

value = df.columns.get_loc('value')
ema = df.columns.get_loc('ema')
alpha = 0.01 #example
for i in range(0, len(df) -1):
    if(i>0):
        df.iat[i, ema] = df.iat[i-1,ema] + alpha*(df.iat[i,value] - df.iat[i-1,ema])
    else:
        df.iat[i, ema] = df.iat[i,value]
azuric
  • 2,679
  • 7
  • 29
  • 44
  • 2
    You [are correct](https://github.com/pandas-dev/pandas/blob/46832ac8f465aa911ba79ebc1b1a4d0f6baf46f9/pandas/_libs/window.pyx#L1539), it is Cythonised. You might want to check out [Divakar's great NumPy answer](https://stackoverflow.com/a/42926270/4686625) for a vectorized NumPy approach. – miradulo Oct 11 '17 at 13:48
  • Possible duplicate of [Numpy version of "Exponential weighted moving average", equivalent to pandas.ewm().mean()](https://stackoverflow.com/questions/42869495/numpy-version-of-exponential-weighted-moving-average-equivalent-to-pandas-ewm) – kosnik May 10 '18 at 16:35

0 Answers0