熊猫单位置索引器出界



我查看了以前关于这个主题的帖子,但没有一个帮助

def trendFinder():
uptrend = pd.DataFrame()
counts = 0
x = 0
for i in range(len(df)):
trendSlot = pd.DataFrame
trendSlot = (df.loc[x+1:x+10])
closeAvg = (sum(trendSlot[:]['Close'])/10)
openAvg = (sum(trendSlot[:]['Open'])/10)
if trendSlot.iloc[-1]['Close'] > closeAvg:
print('close average: ',closeAvg)
print('open average:  ',openAvg)
print(trendSlot)
sleep(0.1)
clear_output()
counts +=1
x+=10
print(counts)

这里基本上发生的事情是,在每次迭代中,都会创建一个具有原始df行/列的迷你数据帧。 然后,对于迷你数据帧,如果最后一行的收盘价大于所有收盘价的平均值,则打印该迷你数据帧。 该函数平稳运行一段时间(我假设直到最后一次迭代(,直到发生产生此错误的事情:

---> 12         if trendSlot.iloc[-1]['Close'] > closeAvg:
...
IndexError: single positional indexer is out-of-bounds

如果向if语句添加条件,例如:

if (len(trendSlot) > 0) and (trendSlot.iloc[-1]['Close'] > closeAvg):

它应该避免尝试执行失败的trendSlot.iloc[-1]以防它是空的。

最新更新