Skip to content

ewm NaN handling is wrong : simple example #31178

@nhuth500

Description

@nhuth500

Hello,

It seems that the handling of NaN values returns bad values on this simple case (I'm using pandas 0.25.3)

import numpy as np
import pandas as pd
x = np.array([1, np.NaN, 5])
alpha = 0.5
x_ewm = pd.Series(x).ewm(alpha=alpha, adjust=False).mean()
x_ewm_simple = alpha * x[2] + ((1 - alpha) **2) * x[0]
print(x_ewm[2])
print(x_ewm_simple)

3.6666666666666665
2.75

According to pandas documentation https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.ewm.html

When ignore_na is False (default), weights are based on absolute positions. For example, the weights of x and y used in calculating the final weighted average of [x, None, y] are (1-alpha)**2 and 1 (if adjust is True), and (1-alpha)**2 and alpha (if adjust is False).

Can you explain how 3.6666666666666665 is computed? Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocsMissing-datanp.nan, pd.NaT, pd.NA, dropna, isnull, interpolateWindowrolling, ewma, expanding

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions