我模拟了从810行开始到821行的逻辑,3支股票,2天的数据,构造了3个df:rs ,cap_sqrt ,ind_citic_lv1
以下这段代码除了构造df,其余都是源代码一样
`import numpy as np
import pandas as pd
'''构造数据'''
rs = pd.DataFrame(np.arange(6).reshape(2, 3), index=('20201201','20201202'), columns=('000001','600039','600519'))
cap_sqrt = pd.DataFrame(np.arange(6).reshape(2, 3)*0.1, index=('20201201','20201202'), columns=('000001','600039','600519'))
ind_citic_lv1 = pd.DataFrame(np.ones(6).reshape(2, 3), index=('20201201','20201202'), columns=('000001','600039','600519'))
ind_citic_lv1.iloc[:,1] = 0
'''以下同源代码'''
dat = pd.DataFrame()
for df in [rs, cap_sqrt, ind_citic_lv1]:
df.index.name = 'time'
df.columns.name = 'code'
dat = pd.concat([dat, df.unstack()], axis=1)
dat.columns = ['rs', 'weight', 'ind']
dat = dat.reset_index()
'''这里会报错'''
rs_ind = {(time, ind): (df['weight'] * df['rs']).sum() / df['weight'].sum()
for time, df_gp in dat.groupby(['time'])
for ind, df in df_gp.groupby(['ind'])}`
代码运行到rs_ind行报错,提示应该是找不到time索引,在执行dat.reset_index()之后,我查看了下dat,如下图
time和code索引在reset_index之后都不见了,是我理解有问题吗?
![image](https://user-images.githubusercontent.com/4597059/112435664-fffbff00-8d7f-11eb-8184-cdea7d349ad2.png)