I have performed a groupby on pandas and I want to apply a complex function which needs several inputs and gives as output a pandas Series that I want to burn in my original dataframe. this is a known procedure to me and has worked very well - that is excpet in this last case (of which I forward my apologies for not being able to post the code in its entirety). essentially I get a TypeError: incompatible index of inserted column with frame index
. but, as shown below, I shouldn't get one.
group_by
part:
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))
where the function is:
def wrapper_new_risk_budget:
print(x.index)
...
print(result.index)
return result.loc[:, 'res']
which raised this error:
raise TypeError('incompatible index of inserted column '
TypeError: incompatible index of inserted column with frame index
the problem is this:
print(np.array_equal(result.index, x.index))
yields all True
. this should be a guarantee of index matching and therefore the problem should not simply be there.
now, I understand the information I am providing is scarce to say the least but do you happen to have any insight on where the problem lies?
p.s.: I have already tried transforming the result in a dataframe and tried to recast the output as pd.Series(result.loc[:, 'res'].values, index=result.index)