I am trying to run Dickey-Fuller test in statsmodels in Python but getting error
Asked Answered
P

3

7

I am trying to run Dickey-Fuller test in statsmodels in Python but getting error P Running from python 2.7 & Pandas version 0.19.2. Dataset is from Github and imported the same

enter code here

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

    print 'Results of Dickey-Fuller Test:'
        dftest = ts.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


    test_stationarity(tr)

Gives me following error :

Results of Dickey-Fuller Test:
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-15-10ab4b87e558> in <module>()
----> 1 test_stationarity(tr)

<ipython-input-14-d779e1ed35b3> in test_stationarity(timeseries)
     19     #Perform Dickey-Fuller test:
     20     print 'Results of Dickey-Fuller Test:'
---> 21     dftest = ts.adfuller(timeseries, autolag='AIC' )
     22     #dftest = adfuller(timeseries, autolag='AIC')
     23     dfoutput = pd.Series(dftest[0:4], index=['Test Statistic','p-value','#Lags Used','Number of Observations Used'])

C:\Users\SONY\Anaconda2\lib\site-packages\statsmodels\tsa\stattools.pyc in adfuller(x, maxlag, regression, autolag, store, regresults)
    209 
    210     xdiff = np.diff(x)
--> 211     xdall = lagmat(xdiff[:, None], maxlag, trim='both', original='in')
    212     nobs = xdall.shape[0]  # pylint: disable=E1103
    213 

C:\Users\SONY\Anaconda2\lib\site-packages\statsmodels\tsa\tsatools.pyc in lagmat(x, maxlag, trim, original)
    322     if x.ndim == 1:
    323         x = x[:,None]
--> 324     nobs, nvar = x.shape
    325     if original in ['ex','sep']:
    326         dropidx = nvar

ValueError: too many values to unpack
Paradigm answered 29/3, 2017 at 17:37 Comment(2)
Your problem isn't with dickey-fuller... your issue is with a tuple not having as many values as you thought.Sergei
I guess timeseries is not a Series. np.asarray(timeseries).ndim > 1 is not supported.Tarantula
J
11

tr must be a 1d array-like, as you can see here. I don't know what is tr in your case. Assuming that you defined tr as the dataframe that contains the time serie's data, you should do something like this:

tr = tr.iloc[:,0].values

Then adfuller will be able to read the data.

Jan answered 10/6, 2017 at 16:5 Comment(0)
D
3

just change the line as:

dftest = adfuller(timeseries.iloc[:,0].values, autolag='AIC' )

It will work. adfuller requires a 1D array list. In your case you have a dataframe. Therefore mention the column or edit the line as mentioned above.

Deflective answered 18/7, 2017 at 10:6 Comment(0)
T
2

I am assuming since you are using the Dickey-Fuller test .you want to maintain the timeseries i.e date time column as index.So in order to do that.

tr=tr.set_index('Month') #I am assuming here the time series column name is Month ts = tr['othercoulumnname'] #Just use the other column name here it might be count or anything

I hope this helps.

Toothed answered 13/1, 2018 at 15:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.