-2
from statsmodels.tsa.stattools import adfuller
def test_stationarity(timeseries):

    #Determing rolling statistics
    rolmean = pd.rolling_mean(timeseries, window=24) # 24 hours on each day
    rolstd = pd.rolling_std(timeseries, window=24)

    #Plot rolling statistics:
    orig = plt.plot(timeseries, 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)

    #Perform Dickey-Fuller test:
    print ('Results of Dickey-Fuller Test:')
    dftest = adfuller(timeseries, autolag='AIC')
    dfoutput = pd.Series(dftest[0:4], index=['Test Statistic','p-value','#Lags Used','Number of Observations Used'])
    for key,value in dftest[4].items():
        dfoutput['Critical Value (%s)'%key] = value
    print (dfoutput)

from matplotlib.pylab import rcParams
rcParams['figure.figsize'] = 20,10

test_stationarity(train_original['Count'])
martineau
  • 119,623
  • 25
  • 170
  • 301
Aashay Bane
  • 11
  • 1
  • 1
  • 1

3 Answers3

4

I'm going to assume that the title error should be AttributeError: module 'pandas' has no attribute 'rolling_mean' as I believe it was deprecated and then removed. Also, pd.rolling is not in the provided code. In which case you SHOULD use rolling. Assuming timeseries is a pd.Series:

def test_stationarity(timeseries):

    #Determing rolling statistics
    rolmean = timeseries.rolling(window=24).mean() # 24 hours on each day
    rolstd = timeseries.rolling(window=24).std()
Ian Thompson
  • 2,914
  • 2
  • 18
  • 31
1

Try doing conda install -c conda-forge statsmodels, based on the earlier syntax, the adfuller was not recognized by the notebook.Then try pip uninstall pandas and do pip install pandas. For similar cases, may try the instructions in the link Numpy is installed but still getting error

Navi
  • 11
  • 1
0
from statsmodels.tsa.stattools import adfuller
def test_stationarity(timeseries):
    #determine rolling statistics
    rolmean = pd.Series(timeseries).rolling(window=24).mean()#24 hours on each day
    rolstd = pd.Series(timeseries).rolling(window=24).std()
    #plot rolling statistics
    orig = plt.plot(timeseries,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 and standard deviation')
    plt.show(block = False)
    #perform dickey fuller test
    print('result of dickey fuller test:')
    dftest = adfuller(timeseries,autolag = 'AIC')
    dfoutput = pd.Series(dftest[0:4],index = ['Test statistics', 'p-value', '#lags used', 'number of observation used'])
    for key,value in dftest[4].items():
        dfoutput['critical value (%s)'%key] = value
    print(dfoutput)

from matplotlib.pylab import rcParams
rcParams['figure.figsize'] = 20,10
test_stationarity(train_original['Count'])