Kailash Hambarde
Kailash Hambarde

Reputation: 29

how to create moving average function with statistics analysis..?

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**

Above I created function which calculate MA window 5. But when I run following code i get error..

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

Upvotes: 2

Views: 391

Answers (2)

James Liu
James Liu

Reputation: 517

I thought we should use

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

Instead of

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

Since pd.rolling_mean is deprecated

EDIT

Just change

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

To

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

EDIT

If you are talking about this line change it from

df['priceModLogMA12'] = pd.rolling_mean(df.priceModLog, window = 5)

To

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

Upvotes: 2

DmytroSytro
DmytroSytro

Reputation: 639

rolling_mean is removed in pandas. Instead you should use pandas.DataFrame.rolling and then apply mean(). Take a look here. You can edit it like this:

ts.rolling(window=12).mean()

Upvotes: 1

Related Questions