Search code examples
pythonpython-3.xpandasdataframetraceback

Traceback using columns in pandas


For some reason the following code from a youtube tutorial gives a traceback due to the commented line. He and other successfully ran it in python 3.

import Quandl
import pandas as pd
import math

df = Quandl.get("WIKI/GOOGL")
df = df[['Adj. Open',  'Adj. High',  'Adj. Low',  'Adj. Close', 'Adj. Volume']]
df['HL_PCT'] = (df['Adj. High'] - df['Adj. Low']) / df['Adj. Close'] * 100.0
df['PCT_change'] = (df['Adj. Close'] - df['Adj. Open']) / df['Adj. Open'] * 100.0

df = df[['Adj. Close', 'HL_PCT', 'PCT_change', 'Adj. Volume']]

forecast_col = 'Adju. Close'
df.fillna(-99999, inplace=True)

forecast_out = int(math.ceil(0.01*len(df)))

df['label'] = df[forecast_col].shift(-forecast_out) #moves columns out 10% into future
df.dropna(inplace=True)
print(df.head())

It gives:

Traceback (most recent call last):
  File "C:\Users\student\Anaconda2\envs\py35\lib\site-packages\pandas\indexes\ba
se.py", line 1945, in get_loc
    return self._engine.get_loc(key)
  File "pandas\index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas
\index.c:4154)
  File "pandas\index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas
\index.c:4018)
  File "pandas\hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.g
et_item (pandas\hashtable.c:12368)
  File "pandas\hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.g
et_item (pandas\hashtable.c:12322)
KeyError: 'Adju. Close'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\student\Desktop\Coursea\MachineLearning\Sentdex\2.py", line 17,
 in <module>
    df['label'] = df[forecast_col].shift(-forecast_out) #moves columns out 10% i
nto future
  File "C:\Users\student\Anaconda2\envs\py35\lib\site-packages\pandas\core\frame
.py", line 1997, in __getitem__
    return self._getitem_column(key)
  File "C:\Users\student\Anaconda2\envs\py35\lib\site-packages\pandas\core\frame
.py", line 2004, in _getitem_column
    return self._get_item_cache(key)
  File "C:\Users\student\Anaconda2\envs\py35\lib\site-packages\pandas\core\gener
ic.py", line 1350, in _get_item_cache
    values = self._data.get(item)
  File "C:\Users\student\Anaconda2\envs\py35\lib\site-packages\pandas\core\inter
nals.py", line 3290, in get
    loc = self.items.get_loc(item)
  File "C:\Users\student\Anaconda2\envs\py35\lib\site-packages\pandas\indexes\ba
se.py", line 1947, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas\index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas
\index.c:4154)
  File "pandas\index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas
\index.c:4018)
  File "pandas\hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.g
et_item (pandas\hashtable.c:12368)
  File "pandas\hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.g
et_item (pandas\hashtable.c:12322)
KeyError: 'Adju. Close'

Currently I four python installations. Two environments in anaconda, which all appear to function well and two 'normal' python installations, 2.7 and 3.5. I've tried installing pandas multiple times in case it was corrupted but it did nothing.


Solution

  • The traceback references the command:

    forecast_col = 'Adju. Close'
    

    which should probably be 'Adj. Close' given that's how the columns seem to be named.