groupby pandas : incompatible index of inserted column with frame index
<p>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 <code>TypeError: incompatible index of inserted column with frame index</code>. but, as shown below, I shouldn't get one.</p>

<p><code>group_by</code> part:</p>

<pre><code>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))
</code></pre>

<p>where the function is:</p>

<pre><code>def wrapper_new_risk_budget:
     print(x.index)
     ...     
     print(result.index)
     return result.loc[:, 'res']
</code></pre>

<p>which raised this error:</p>

<pre><code>    raise TypeError('incompatible index of inserted column '
TypeError: incompatible index of inserted column with frame index
</code></pre>

<p>the problem is this:</p>

<pre><code>print(np.array_equal(result.index, x.index))
</code></pre>

<p>yields all <code>True</code>. this should be a guarantee of index matching and therefore the problem should not simply be there.</p>

<p>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>

<p>p.s.: I have already tried transforming the result in a dataframe and tried to recast the output as <code>pd.Series(result.loc[:, 'res'].values, index=result.index)</code></p>

<p>ok, for reasons beyond my understanding, when I performed a merge inside the code, although their numpy representation was equivalent, they differed for something else before pandas' eyes. I tried a work-around of the merge (longer and more inefficient) and now with more traditional means it works.</p>

<p>today I won't be able to post the complete example since I am very pressed for time and I have a deadline looming over but I will complete it as soon as possible both to show respect to those who have answered or tried to do so and to all the other users who might find something beneficial in the resolution of this problem</p>

python pandas indexing group-by