Python 熊猫错误:“;pandas.libs.hashtable.PyObjectHashTable.get_项(pandas/_libs/hashtable.c:20477)

Python 熊猫错误:“;pandas.libs.hashtable.PyObjectHashTable.get_项(pandas/_libs/hashtable.c:20477),python,python-2.7,pandas,numpy,sklearn-pandas,Python,Python 2.7,Pandas,Numpy,Sklearn Pandas,首先,大家好。我已经完成了一半,我有一个csv,其中包含一些历史股票数据,我正在读取到熊猫中: def process_data_for_labels(ticker): hm_days = 7 df = pd.read_csv('file.csv', index_col=0) tickers = df.columns.values.tolist() df.fillna(0, inplace=True) for i in range(1, hm_days + 1)

首先,大家好。我已经完成了一半,我有一个csv,其中包含一些历史股票数据,我正在读取到熊猫中:

def process_data_for_labels(ticker):
    hm_days = 7
    df = pd.read_csv('file.csv', index_col=0)
    tickers = df.columns.values.tolist()
    df.fillna(0, inplace=True)

for i in range(1, hm_days + 1):
    df['{}_{}d'.format(ticker, i)] = (df[ticker].shift(-i) - df[ticker]) / df[ticker]

df.fillna(0, inplace=True)
return tickers, df

def buy_sell_hold(*args):
    cols = [c for c in args]
    requirement = 0.02
    for col in cols:
        if col > requirement:
            # Buy = 1 , Sell = 1 , Hold = 0
            return 1
        if col < -requirement:
            return -1
    return 0


def extract_featuresets(ticker):
    tickers, df = process_data_for_labels(ticker)

    df['{}_target'.format(ticker)] = list(map(buy_sell_hold,
                                          df['{}_1d'.format(ticker)],
                                          df['{}_2d'.format(ticker)],
                                          df['{}_3d'.format(ticker)],
                                          df['{}_4d'.format(ticker)],
                                          df['{}_5d'.format(ticker)],
                                          df['{}_6d'.format(ticker)],
                                          df['{}_7d'.format(ticker)]
                                          ))

vals = df['{}_target'.format(ticker)].values.tolist()
str_vals = [str(i) for i in vals]
print('Data spread:', Counter(str_vals))

df.fillna(0, inplace=True)
df = df.replace([np.inf, -np.inf], np.nan)
df.dropna(inplace=True)

df_vals = df[[ticker for ticker in tickers]].pct_change()
df_vals = df_vals.replace([np.inf, -np.inf], 0)
df_vals.fillna(0, inplace=True)

X = df_vals.values
y = df['{}_target'.format(ticker)].values

return X, y, df

extract_featuresets('XOM')
从错误的外观来看,我认为它是在抱怨ZTS列数据,尝试以不同的方式格式化它,当其他所有操作都失败时,我尝试删除它,然后错误转到了它前面的公司列数据

如果使用
extract\u featureset('ZTS')
运行该函数,它不会失败,但所有其他的ticker都会失败

有没有办法解决这个问题?谢谢

也有同样的错误, 更改为python 3.6。 它会神奇地消失

File "pandas\_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5280)
  File "pandas\_libs\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5126)
  File "pandas\_libs\hashtable_class_helper.pxi", line 1210, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20523)
  File "pandas\_libs\hashtable_class_helper.pxi", line 1218, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20477)
KeyError: 'ZTS_target'