索引越界python
我正在编写一个小的Python代码来测试股票的买入/卖出策略,我得到了这个指数的越界错误,但只有一些值 错误日志:索引越界python,python,pandas,numpy,Python,Pandas,Numpy,我正在编写一个小的Python代码来测试股票的买入/卖出策略,我得到了这个指数的越界错误,但只有一些值 错误日志: KeyError Traceback (most recent call last) ~\Anaconda3\envs\finance\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key) 3102
KeyError Traceback (most recent call last)
~\Anaconda3\envs\finance\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key)
3102 return self._engine.get_value(s, k,
-> 3103 tz=getattr(series.dtype, 'tz', None))
3104 except KeyError as e1:
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
KeyError: -1
During handling of the above exception, another exception occurred:
IndexError Traceback (most recent call last)
<ipython-input-81-3e66a7abf00d> in <module>()
20 for i, s in enumerate(short):
21 for j, l in enumerate(long):
---> 22 rets, sharpe = ma_strat( s, l)
23 profit[i,j] = rets
24 sharperat[i,j] = sharpe
<ipython-input-81-3e66a7abf00d> in ma_strat(short_, long_)
9 datos['strategy'] = datos['ret'] * datos['pos'].shift(1)
10 sh = np.sqrt(252) * (datos['strategy'].mean() / datos['strategy'].std())
---> 11 rets = datos['strategy'].cumsum()[-1]
12 return rets, sh
13
~\Anaconda3\envs\finance\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
764 key = com._apply_if_callable(key, self)
765 try:
--> 766 result = self.index.get_value(self, key)
767
768 if not is_scalar(result):
~\Anaconda3\envs\finance\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key)
3107
3108 try:
-> 3109 return libindex.get_value_box(s, key)
3110 except IndexError:
3111 raise
pandas\_libs\index.pyx in pandas._libs.index.get_value_box()
pandas\_libs\index.pyx in pandas._libs.index.get_value_box()
IndexError: index out of bounds
但是如果我将值设置为:(在下面的代码中)
不起作用。显示索引越界错误
def ma_strat(short_, long_):
datos['sh'] = ta.EMA(datos['Adj Close'].values, short_)
datos['lg'] = ta.EMA(datos['Adj Close'].values, long_)
datos.dropna(inplace=True)
datos['pos'] = np.where(datos['sh']>datos['lg'], 1, 0)
datos['ret'] = np.log(datos['Adj Close'] / datos['Adj Close'].shift(1))
datos['strategy'] = datos['ret'] * datos['pos'].shift(1)
sh = np.sqrt(252) * (datos['strategy'].mean() / datos['strategy'].std())
rets = datos['strategy'].cumsum()[-1]
return rets, sh
short = np.linspace(3, 13, 11, dtype=int)
print(short)
long = np.linspace(13, 23, 11, dtype=int)
print(long)
profit = np.zeros((len(short), len(long)))
sharperat = np.zeros((len(short), len(long)))
for i, s in enumerate(short):
for j, l in enumerate(long):
rets, sharpe = ma_strat( s, l)
profit[i,j] = rets
sharperat[i,j] = sharpe
看起来像是
datos['strategy']。cumsum()[-1]
正在返回熊猫系列。这就是为什么[-1]
索引可能无法工作的原因
尝试np.array(datos['strategy']).cumsum()[-1]
希望这有帮助看起来像是
datos['strategy'].cumsum()[-1]
正在返回熊猫系列。这就是为什么[-1]
索引可能无法工作的原因
尝试np.array(datos['strategy']).cumsum()[-1]
希望这有帮助你能发布一个完整的回溯吗?你能发布一个我们可以测试的简单工作示例吗?你能发布一个完整的回溯吗?你能发布一个我们可以测试的简单工作示例吗?你能将整个错误日志粘贴到这里吗。会更有帮助。出于好奇,
ma_strat(s,l)
是否与ma_策略(short,long)
相同?完成,是相同,粘贴代码时出错。第一件事-1的关键错误是因为您在熊猫系列上使用了cumsum()[-1]。该异常导致索引越界异常。您是否可以打印类型(datos['strategy'])
的输出,并尝试在您的函数中使用rets=datos['strategy'].values.cumsum()[-1]
。输出:如果我更改代码行,则会出现以下错误:索引器:索引-1超出大小为0的轴0的界限,即';这是根本原因。检查列返回NaN值的原因,更正并填充相关值,然后使用答案中提供的解决方案。您可以将整个错误日志粘贴到此处吗。会更有帮助。出于好奇,ma_strat(s,l)
是否与ma_策略(short,long)
相同?完成,是相同,粘贴代码时出错。第一件事-1的关键错误是因为您在熊猫系列上使用了cumsum()[-1]。该异常导致索引越界异常。您是否可以打印类型(datos['strategy'])
的输出,并尝试在您的函数中使用rets=datos['strategy'].values.cumsum()[-1]
。输出:如果我更改代码行,则会出现以下错误:索引器:索引-1超出大小为0的轴0的界限,即';这是根本原因。检查列返回NaN值的原因,纠正并填充相关值,然后使用答案中提供的解决方案。
def ma_strat(short_, long_):
datos['sh'] = ta.EMA(datos['Adj Close'].values, short_)
datos['lg'] = ta.EMA(datos['Adj Close'].values, long_)
datos.dropna(inplace=True)
datos['pos'] = np.where(datos['sh']>datos['lg'], 1, 0)
datos['ret'] = np.log(datos['Adj Close'] / datos['Adj Close'].shift(1))
datos['strategy'] = datos['ret'] * datos['pos'].shift(1)
sh = np.sqrt(252) * (datos['strategy'].mean() / datos['strategy'].std())
rets = datos['strategy'].cumsum()[-1]
return rets, sh
short = np.linspace(3, 13, 11, dtype=int)
print(short)
long = np.linspace(13, 23, 11, dtype=int)
print(long)
profit = np.zeros((len(short), len(long)))
sharperat = np.zeros((len(short), len(long)))
for i, s in enumerate(short):
for j, l in enumerate(long):
rets, sharpe = ma_strat( s, l)
profit[i,j] = rets
sharperat[i,j] = sharpe