gpt4 book ai didi

python - Pandas 错误 : “pandas. libs.hashtable.PyObjectHashTable.get_item (pandas/_libs/hashtable.c:20477)

转载 作者:行者123 更新时间:2023-11-28 19:07:29 34 4
gpt4 key购买 nike

首先,大家好。我已经中途了Python Programming for Finance - Creating targets for machine learning labels ,我有一个 csv,其中包含我正在读入 pandas 的一些历史股票数据:

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')

但是,当我运行它时,出现以下错误:

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'

从错误的外观来看,我认为它是在提示 ZTS 列数据,尝试以不同的方式对其进行格式化,当其他一切都失败时,我尝试将其删除,然后错误转到了 Company 列数据就在它之前。

如果您使用 extract_featuresets('ZTS') 运行函数,它不会失败,但所有其他代码都会失败。

有什么解决办法吗?谢谢!

最佳答案

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

关于python - Pandas 错误 : “pandas. libs.hashtable.PyObjectHashTable.get_item (pandas/_libs/hashtable.c:20477),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45246668/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com