Python 模块 'pandas' 没有属性 'rolling_mean'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50482884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:30:22  来源:igfitidea点击:

module 'pandas' has no attribute 'rolling_mean'

pythonpandasmachine-learningtime-seriesarima

提问by Pruce Uchiha

I am trying to build a ARIMA for anomaly detection. I need to find the moving average of the time series graph I am trying to use pandas 0.23 for this

我正在尝试构建一个用于异常检测的 ARIMA。我需要找到时间序列图的移动平均值我正在尝试为此使用 pandas 0.23

import pandas as pd
import numpy as np
from statsmodels.tsa.stattools import adfuller
import matplotlib.pylab as plt
from matplotlib.pylab import rcParams
rcParams['figure.figsize'] = 15, 6

dateparse = lambda dates: pd.datetime.strptime(dates, '%Y-%m')
data = pd.read_csv('AirPassengers.csv', parse_dates=['Month'], index_col='Month',date_parser=dateparse)

data.index
ts = data['#Passengers']
ts.head(10)

plt.plot(ts)
ts_log = np.log(ts)
plt.plot(ts_log)
moving_avg = pd.rolling_mean(ts_log,12)  # here is the error

pd.rolling_mean  
plt.plot(ts_log)
plt.plot(moving_avg, color='red') 

error:Traceback (most recent call last): File "C:\Program Files\Python36\lastmainprogram.py", line 74, in moving_avg = pd.rolling_mean(ts_log,12) AttributeError: module 'pandas' has no attribute 'rolling_mean'

错误:回溯(最近一次调用):文件“C:\ '

回答by jezrael

I believe need change:

我相信需要改变:

moving_avg = pd.rolling_mean(ts_log,12)

to:

到:

moving_avg = ts_log.rolling(12).mean()

because old pandas version code below pandas 0.18.0

因为下面的旧熊猫版本代码 pandas 0.18.0

回答by Brahmaiahchowdary

Change:

改变:

moving_avg = pd.rolling_mean(ts_log,12)

to:

到:

rolmean = pd.Series(timeseries).rolling(window=12).mean()

rolstd = pd.Series(timeseries).rolling(window=12).std()

回答by Mehmet Ozden

You will need this in your detection so you can add.

您将在检测中需要它,以便您可以添加。

moving_std = ts_log.rolling(12).std()