【问题标题】:Traceback using columns in pandas使用 pandas 中的列进行追溯
【发布时间】:2016-05-24 23:44:15
【问题描述】:

出于某种原因,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\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'

目前我安装了四个 python。 anaconda 中的两个环境似乎都运行良好,还有两个“正常”的 python 安装,2.7 和 3.5。我已经尝试多次安装pandas,以防它被损坏,但它什么也没做。

【问题讨论】:

    标签: python python-3.x pandas dataframe traceback


    【解决方案1】:

    回溯引用命令:

    forecast_col = 'Adju. Close'
    

    这应该是'Adj. Close',因为columns 似乎是这样命名的。

    【讨论】:

    • 这个操作让我感到困惑的是,如果我们改变了 Adj.关闭列到未来 10 天(我猜只是将第 1-10 天的值复制到第 11-20 天),我们没有 ''HL_PCT'、'PCT_change'、'Adj.未来 10 天的交易量。如果是这种情况,X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.2) 将如何在未来 10 天内为label 提供'HL_PCT', 'PCT_change', 'Adj. Volume' 的相应值?其中 X 和 y 是 X = np.array(df.drop(['label'], 1)) y = np.array(df['label'])
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-01
    • 1970-01-01
    • 2021-01-07
    • 2013-08-02
    • 2018-12-28
    • 2013-01-26
    相关资源
    最近更新 更多