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

时间:2018-05-23 07:57:39

标签: python pandas machine-learning time-series arima

我正在尝试构建用于异常检测的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') 
  

错误:回溯(最近一次调用最后一次):文件“C:\ Program   Files \ Python36 \ lastmainprogram.py“,第74行,in       moving_avg = pd.rolling_mean(ts_log,12)AttributeError:模块'pandas'没有属性'rolling_mean'

4 个答案:

答案 0 :(得分:58)

我认为需要改变:

moving_avg = pd.rolling_mean(ts_log,12)

为:

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

因为旧的pandas版本代码低于pandas 0.18.0

答案 1 :(得分:4)

更改:

moving_avg = pd.rolling_mean(ts_log,12)

收件人:

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

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

答案 2 :(得分:1)

更改:

moving_avg = pd.rolling_mean(ts_log,12)

收件人:

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

答案 3 :(得分:0)

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

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