Python 使用pandas中的列进行回溯

Python 使用pandas中的列进行回溯,python,python-3.x,pandas,dataframe,traceback,Python,Python 3.x,Pandas,Dataframe,Traceback,出于某种原因,以下来自youtube教程的代码对注释行进行了回溯。他和其他人在Python3中成功地运行了它 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['

出于某种原因,以下来自youtube教程的代码对注释行进行了回溯。他和其他人在Python3中成功地运行了它

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\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'
回溯(最近一次呼叫最后一次):
文件“C:\Users\student\Anaconda2\envs\py35\lib\site packages\pandas\index\ba
se.py“,第1945行,在get_loc
返回发动机。获取位置(钥匙)
文件“pandas\index.pyx”,第137行,在pandas.index.IndexEngine.get_loc(pandas)中
\索引c:4154)
pandas.index.IndexEngine.get_loc(pandas)中的文件“pandas\index.pyx”,第159行
\索引c:4018)
pandas.hashtable.PyObjectHashTable.g中的文件“pandas\hashtable.pyx”,第675行
et_项(熊猫\hashtable.c:12368)
pandas.hashtable.PyObjectHashTable.g中的文件“pandas\hashtable.pyx”,第683行
et_项(熊猫\hashtable.c:12322)
KeyError:'调整。接近
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“C:\Users\student\Desktop\Coursea\MachineLearning\Sentdex\2.py”,第17行,
在里面
df['label']=df[forecast_col].shift(-forecast_out)#将列移出10%i
nto未来
文件“C:\Users\student\Anaconda2\envs\py35\lib\site packages\pandas\core\frame
.py”,第1997行,在__
返回self.\u getitem\u列(键)
文件“C:\Users\student\Anaconda2\envs\py35\lib\site packages\pandas\core\frame
.py”,第2004行,在_getitem_列中
返回self.\u获取\u项目\u缓存(密钥)
文件“C:\Users\student\Anaconda2\envs\py35\lib\site packages\pandas\core\gener
ic.py“,第1350行,在获取项目缓存中
values=self.\u data.get(项目)
文件“C:\Users\student\Anaconda2\envs\py35\lib\site packages\pandas\core\inter
nals.py“,第3290行,在get中
loc=自身项目。获取loc(项目)
文件“C:\Users\student\Anaconda2\envs\py35\lib\site packages\pandas\index\ba
se.py”,第1947行,在get_loc中
返回self.\u引擎。获取\u loc(self.\u可能\u cast\u索引器(键))
文件“pandas\index.pyx”,第137行,在pandas.index.IndexEngine.get_loc(pandas)中
\索引c:4154)
pandas.index.IndexEngine.get_loc(pandas)中的文件“pandas\index.pyx”,第159行
\索引c:4018)
pandas.hashtable.PyObjectHashTable.g中的文件“pandas\hashtable.pyx”,第675行
et_项(熊猫\hashtable.c:12368)
pandas.hashtable.PyObjectHashTable.g中的文件“pandas\hashtable.pyx”,第683行
et_项(熊猫\hashtable.c:12322)
KeyError:'调整。接近

目前我安装了四个python。anaconda中的两个环境都运行良好,还有两个“正常”python安装,即2.7和3.5。我已多次尝试安装pandas,以防它损坏,但没有任何效果。

回溯引用了以下命令:

forecast_col = 'Adju. Close'

这可能是
'Adj。关闭“
,因为这是
列的命名方式。

这个操作让我困惑的是,如果我们移动了Adj。将列关闭到未来10天(我想只是复制第1-10天到第11-20天的值),我们没有“HL\u PCT”、“PCT\u change”、“Adj”的值。未来10天内的“卷”。如果是这种情况,
X\u-train,X\u-test,y\u-train,y\u-test=cross\u验证。train\u-test\u-split(X,y,test\u-size=0.2)
将如何提供相应的值
'HL\u-PCT',PCT\u-change',Adj。卷“
用于
标签
未来10天?其中X和y是
X=np.array(df.drop(['label'],1))y=np.array(df['label'])