使用 pandas 中的列进行回溯
Traceback using columns in pandas
出于某种原因,YouTube 教程中的以下代码由于注释行而给出了回溯。他和其他人成功 运行 它在 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())
它给出:
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.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'
目前我有四个 python 安装。 anaconda 中的两个环境,它们似乎都运行良好,还有两个 'normal' python 安装,2.7 和 3.5。我已经尝试多次安装 pandas
以防它损坏,但它什么也没做。
回溯引用命令:
forecast_col = 'Adju. Close'
可能应该是 'Adj. Close'
,因为 columns
似乎就是这样命名的。
出于某种原因,YouTube 教程中的以下代码由于注释行而给出了回溯。他和其他人成功 运行 它在 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())
它给出:
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.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'
目前我有四个 python 安装。 anaconda 中的两个环境,它们似乎都运行良好,还有两个 'normal' python 安装,2.7 和 3.5。我已经尝试多次安装 pandas
以防它损坏,但它什么也没做。
回溯引用命令:
forecast_col = 'Adju. Close'
可能应该是 'Adj. Close'
,因为 columns
似乎就是这样命名的。