我试图在Python的statsmodels中运行Dickey-Fuller测试,但是收到错误

N.R*_*N.R 5 python-2.7 pandas jupyter-notebook

我试图在Python的statsmodels中运行Dickey-Fuller测试但是从python 2.7和Pandas版本0.19.2得到错误P Running.数据集来自Github并导入相同

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)
Run Code Online (Sandbox Code Playgroud)

给我以下错误:

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
Run Code Online (Sandbox Code Playgroud)

小智 9

TR必须是一维数组类,你可以看到在这里.我不知道什么是TR你的情况.假设您将tr定义为包含时间系列数据的数据帧,您应该执行以下操作:

tr = tr.iloc[:,0].values
Run Code Online (Sandbox Code Playgroud)

然后adfuller将能够读取数据.