Python 如何使用统计分析创建移动平均线函数。。?

Python 如何使用统计分析创建移动平均线函数。。?,python,pandas,statistics,time-series,moving-average,Python,Pandas,Statistics,Time Series,Moving Average,上面我创建了计算MA窗口5的函数。 但是当我运行下面的代码时,我得到了一个错误 def adf(ts): # Determing rolling statistics rolmean = pd.rolling_mean(ts, window=12) rolstd = pd.rolling_std(ts, window=12) #Plot rolling statistics: orig = plt.plot(ts, color='blue',label='Original') mean = p

上面我创建了计算MA窗口5的函数。 但是当我运行下面的代码时,我得到了一个错误

def adf(ts):

# Determing rolling statistics
rolmean = pd.rolling_mean(ts, window=12)
rolstd = pd.rolling_std(ts, window=12)
#Plot rolling statistics:
orig = plt.plot(ts, color='blue',label='Original')
mean = plt.plot(rolmean, color='red', label='Rolling Mean')
std = plt.plot(rolstd, color='black', label = 'Rolling Std')
plt.legend(loc='best')
plt.title('Rolling Mean & Standard Deviation')
plt.show(block=False)

# Calculate ADF factors
adftest = adfuller(ts, autolag='AIC')
adfoutput = pd.Series(adftest[0:4], index=['Test Statistic','p-value','# of Lags Used',
                                          'Number of Observations Used'])
for key,value in adftest[4].items():
    adfoutput['Critical Value (%s)'%key] = value
return adfoutput**

我想我们应该使用

df['priceModLogMA12'] = pd.rolling_mean(df.priceModLog, window = 5)**
AttributeError: module 'pandas' has no attribute 'rolling_mean'
而不是

rolmean = ts.rolling(window=12).mean()
因为pd.rolling_mean已被弃用

编辑

换衣服

rolmean = pd.rolling_mean(ts, window=12)

编辑

如果你正在谈论这一行,请将其从

rolmean = ts.rolling(window=12).mean()
rolstd = ts.rolling(window=12).std()


大熊猫的滚动平均值被删除。相反,您应该使用pandas.DataFrame.rolling,然后应用mean()。看一看。您可以这样编辑它:

df['priceModLogMA12'] = df.priceModLog.rolling(window = 5).mean()

谢谢您,先生,但是我如何更改我的上述功能,以便我可以使用它@劉炳瓚
df['priceModLogMA12'] = pd.rolling_mean(df.priceModLog, window = 5)
df['priceModLogMA12'] = df.priceModLog.rolling(window = 5).mean()
ts.rolling(window=12).mean()