我正在使用for循环合并3个数据帧,但仅返回最后一个df
我已经阅读了python-guide的陷阱和Python Pocket Reference。我了解问题出在“默认值”
请忽略下面的缩进问题。该功能否则定义正确
def hkmultiday(code, date1asstring, date2asstring):
x = pd.bdate_range(date1asstring, date2asstring)
#there are 2 business dates between the 2 input dates
df2=pd.DataFrame([[datetime.datetime.now(),0,0,0,0,0,0,0]],columns=['date','open','high','low','close','volume','barCount','average'])
#i created an empty dataframe for "append" function. using the append function allows me to organize the final dataframes by decreasing dates
for date in x.date:
y=datetime.datetime(year=date.year,month=date.month,day=date.day,hour=16,minute=0,second=1)
df2=hkday(code,y).append(df2, ignore_index=True)
#hkday is a custom function defined outside hkmultiday. It returns a dataframe for one-day stock data.
return df2
预期: 空数据框+第1天数据框+第2天数据框
实际: 空数据框+第2天数据框我怀疑它与“默认值”有关,并且我需要在上面的代码中的某处添加“ df2 = df2”。但是我不知道在哪里。在hkday.append()中添加df2 = df2会产生错误。谢谢