简体   繁体   中英

Issue with def function

I'm trying to create Exponential Moving average. However, the error code 'Series' object is not callable' appeared. This is my code. Can someone help?

def CalculateEMA(window):

    sma = Close.rolling(window, min_periods=window).mean()[:window]
    rest = Close[window:]
    EMA_window=pd.concat([sma, rest]).ewm(span=window, adjust=False).mean()

    return EMA_window()
CalculateEMA(60)

You are calling the pandas.Series that is returned from the mean method. This doesnt make any sense and since Series doesn't implement __call__ it fails with an error.

What you probably want to do is remove the parens and just return the Series itself.

def CalculateEMA(window):

    sma = Close.rolling(window, min_periods=window).mean()[:window]
    rest = Close[window:]
    EMA_window=pd.concat([sma, rest]).ewm(span=window, adjust=False).mean()

    return EMA_window # <-- changed here
CalculateEMA(60)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM