【发布时间】:2017-01-16 00:30:28
【问题描述】:
我在 pandas 上执行了 groupby,我想应用一个复杂的函数,该函数需要多个输入,并提供一个我想在原始数据帧中刻录的 pandas Series 作为输出。这对我来说是一个已知的程序,并且运行良好 - 在最后一种情况下除外(我对无法完整发布代码表示歉意)。基本上我得到了TypeError: incompatible index of inserted column with frame index。但是,如下所示,我不应该得到一个。
group_by 部分:
all_in_data_risk['weights_of_the_sac'] = all_in_data_risk.groupby(['ptf', 'ac'])['sac', 'unweighted_weights_by_sac', 'instrument_id', 'risk_budgets_sac'].apply(lambda x: wrapper_new_risk_budget(x, temp_fund_all_ret, method_compute_cov))
函数在哪里:
def wrapper_new_risk_budget:
print(x.index)
...
print(result.index)
return result.loc[:, 'res']
引发了这个错误:
raise TypeError('incompatible index of inserted column '
TypeError: incompatible index of inserted column with frame index
问题是这样的:
print(np.array_equal(result.index, x.index))
产生所有True。这应该是索引匹配的保证,因此问题不应该只是存在。
现在,我知道我提供的信息很难说,但您是否对问题所在有任何见解?
ps:我已经尝试将结果转换为数据帧并尝试将输出重铸为pd.Series(result.loc[:, 'res'].values, index=result.index)
【问题讨论】:
标签: python pandas indexing group-by