0

need some help here to eliminate this error. Here is the code - the # was the original code which running wonderful and I tried to adapt:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.stattools import adfuller
from genhurst import genhurst
from datetime import datetime
import pandas_datareader as pdr 


df = pdr.DataReader('BTC-USD', 'yahoo', datetime(2014,1,1), datetime(2019,10,25))

# df=pd.read_csv('inputData_USDCAD.csv')

y=df.loc[df['Adj Close']]

# y=df.loc[df['Time']==1659, 'Close']


results=adfuller(y, maxlag=1, regression='c', autolag=None)
print(results)

# Find Hurst exponent
H, pVal=genhurst(np.log(y))
print("H=%f pValue=%f" % (H, pVal))

Those are the error messages:

File "", line 14, in y=df.loc[df['Adj Close']]

File "C:\Users\apros\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 1424, in getitem return self._getitem_axis(maybe_callable, axis=axis)

File "C:\Users\apros\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 1839, in _getitem_axis return self._getitem_iterable(key, axis=axis)

File "C:\Users\apros\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 1133, in _getitem_iterable keyarr, indexer = self._get_listlike_indexer(key, axis, raise_missing=False)

File "C:\Users\apros\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 1089, in _get_listlike_indexer keyarr, indexer, new_indexer = ax._reindex_non_unique(keyarr)

File "C:\Users\apros\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 3443, in _reindex_non_unique indexer, missing = self.get_indexer_non_unique(target)

File "C:\Users\apros\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 4801, in get_indexer_non_unique indexer, missing = self._engine.get_indexer_non_unique(tgt_values)

File "pandas_libs\index.pyx", line 295, in pandas._libs.index.IndexEngine.get_indexer_non_unique

TypeError: 'NoneType' object is not iterable

4
  • 1
    whats the content of df['Adj Close'] what do you see if you do print(type(df['Adj Close']), "\n", df['Adj Close']) Commented Nov 7, 2019 at 9:34
  • what's in the column 'Adj Close'? if it's not boolean, doing df.loc[df['Adj Close']] is probably wrong - i guess you probably want df.loc[df['Adj Close'] == 'some condition']? Commented Nov 7, 2019 at 9:35
  • I had a look at the data set and df['Adj Close'] is a series of floats. So I am not sure what you are expecting to do with the loc() method here when you are not filtering on any condition. Maybe edit your question to explain what your trying to achieve Commented Nov 7, 2019 at 9:40
  • for example if you wanted to select into y all rows which the Adj Close value is less than 200 then you can do y=df.loc[df['Adj Close'] < 200] this will return you a df of [2 rows x 6 columns] Commented Nov 7, 2019 at 9:42

2 Answers 2

0

Your code shows that you have read your df from pdr.DataReader.

But this is too little information to say anything about the source of your error. Actually, the way how you read the df is of very little importance here.

Rather show a piece of your df.

An important detail is also what are data types of each column, especially that used as the criterion field (Adj Close).

I suppose that it has been read as string (or some outher type), but you expect a boolean type there.

Mabe you should compare the value in Adj Close column with some concrete value?

Check also whether your df actually contains a column with this name.

Sign up to request clarification or add additional context in comments.

Comments

0

thank you guys!!

the .loc method was the problem, it seems to work by applying y = df['Adj Close'] only`

@author: Ernest
"""

# Example 2: Using ADF Test for Mean Reversion
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import statsmodels.api as sm
import pandas_datareader as pdr 
from statsmodels.tsa.stattools import adfuller
from datetime import datetime



def genhurst(z):
# =============================================================================
# calculation of Hurst exponent given log price series z
# =============================================================================
    z=pd.DataFrame(z)

    taus=np.arange(np.round(len(z)/10)).astype(int) # We cannot use tau that is of same magnitude of time series length 
    logVar=np.empty(len(taus)) # log variance

    for tau in taus:
        logVar[tau]=np.log(z.diff(tau).var(ddof=0))

    X=np.log(taus)    
    Y=logVar[:len(taus)]
    X=X[np.isfinite(logVar)]
    Y=Y[np.isfinite(logVar)]
#    pd.DataFrame(np.asmatrix([X, Y]).T).to_csv('XY.csv')

    X = sm.add_constant(X)
#    plt.scatter(X[:,1], Y) # for debug only
    model=sm.OLS(Y, X)
    results=model.fit()
    H=results.params[1]/2
    pVal=results.pvalues[1]
    return H, pVal


df = pdr.DataReader('BCH-USD', 'yahoo', datetime(2017,1,1), datetime(2019,10,25))

# df=pd.read_csv('inputData_USDCAD.csv')

y = df['Adj Close']

# y=df.loc[df['Time']==1659, 'Close']


results=adfuller(y, maxlag=1, regression='c', autolag=None)
print(results)

# Find Hurst exponent
H, pVal=genhurst(np.log(y))
print("H=%f pValue=%f" % (H, pVal))

BR Andreas

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.