Python While循环内更新数据帧错误:仅整数、切片(`:`)、省略号(`…`)、numpy.newaxis(`None`)和整数或布尔数组有效

Python While循环内更新数据帧错误:仅整数、切片(`:`)、省略号(`…`)、numpy.newaxis(`None`)和整数或布尔数组有效,python,pandas,numpy,Python,Pandas,Numpy,我无法在while循环中获取要更新的数据帧。while循环需要为6个值运行,并使用以前的值更新以前的数据集(删除最旧的数据集,添加最新的数据集)。任何帮助都将不胜感激 # Multi-step forecast # next_6days_stock_price validation_target = pd.to_numeric(['4185.47', '4163.26', '4134.94', '4173.42', '4134.98', '4180.17'], errors='coerce'

我无法在while循环中获取要更新的数据帧。while循环需要为6个值运行,并使用以前的值更新以前的数据集(删除最旧的数据集,添加最新的数据集)。任何帮助都将不胜感激

# Multi-step forecast

# next_6days_stock_price 
validation_target = pd.to_numeric(['4185.47', '4163.26', '4134.94', '4173.42', '4134.98', '4180.17'], errors='coerce')
validation_predictions = []

#use the last 60 days to predict the next one into the future
#get the last 60 days close price and convert the dataframe to an array

#scale the data to be values between 0 and 1
a = data[['close']]
d = a[-n-1:]


while len(validation_predictions) < len(validation_target):   
  
  d['PrevClose'] = d['close'].shift(1)
  d['returns'] = (d['close'] - d['PrevClose']) / d['PrevClose']
  d['log_returns'] = np.log(1 + d['returns'])
  d = d[['close', 'log_returns']]
  
  last_m_d = scaler.transform(m)
  
  pred_price2 = model.predict(last_m_d.reshape(1, n, 2 )) # 2x1 array -> scalar
  pred_price2 = np.c_[pred_price2, np.zeros(pred_price2.shape)]
  pred_price2 = scaler.inverse_transform(pred_price2)
  pred_price2 = pred_price2[:1,0]
 
  # update the predictions list
  validation_predictions.append(pred_price2)
  p1 = np.array(validation_predictions)
  p1 = p1.reshape((p1.shape[0], -1), order='F')
  # make the new input
  d = np.roll(d, -1)
  d[-1] = pred_price2

#多步预测
#未来6天股票价格
验证目标=pd.到数值(['4185.47','4163.26','4134.94','4173.42','4134.98','4180.17',错误='强制')
验证\u预测=[]
#用过去的60天来预测未来的下一天
#获取最近60天的收盘价,并将数据帧转换为数组
#将数据缩放为介于0和1之间的值
a=数据[['close']]
d=a[-n-1:]
而len(验证预测)标量
pred_price2=np.c_[pred_price2,np.zero(pred_price2.shape)]
pred_price2=定标器逆变换(pred_price2)
价格预测2=价格预测2[:1,0]
#更新预测列表
验证\u预测。追加(pred\u价格2)
p1=np.数组(验证\u预测)
p1=p1.重塑((p1.形状[0],-1),顺序='F')
#进行新的输入
d=np.滚动(d,-1)
d[-1]=预定价2

您没有告诉我们错误发生的位置。它看起来像是用字符串索引numpy数组时产生的那种错误。字符串索引数据帧列,但不是从帧派生的数组。错误发生在while循环后的前四个步骤中。当我将它们移出while循环时,代码会工作,但会给出相同的值6次。但是,我需要6个不同的值,其中追加上次运行的每个输出,删除一个最旧的值,并迭代运行代码。看起来
d
最初是一个接受列名的
DataFrame
。但是在
while
中,它将更改为
numpy
数组,该数组不适用于名称。跟踪变量赋值和类型是Python调试的一个基本部分。@hpaulj-我理解。您建议我如何更正此代码以使其进行迭代,而不会出现将其类型在then循环中更改为numpy数组的错误?我还没有对其进行测试,但是
np.roll
可能是问题所在。一些
numpy
函数保留数据帧,但不是全部。这是调试应该发现的事情。