When you write
a.loc[1,'b'] = b
and b is a series, index b must exactly match the index generated by a.loc[1,'b'] so that the values ββin b are copied to a . It turns out, however, that if a.columns is MultiIndex , the index for a.loc[1,'b'] :
(Pdb) p new_ix Index([(u'b', 0), (u'b', 1)], dtype='object')
whereas the index for b is
(Pdb) p ser.index Int64Index([0, 1], dtype='int64')
They do not match, and therefore
(Pdb) p ser.index.equals(new_ix) False
Since the values ββare not aligned, the code branch you fall into assigns
(Pdb) p ser.reindex(new_ix).values array([ nan, nan])
I found this by adding pdb.set_trace() to your code:
import pandas as pd columns = pd.MultiIndex.from_product([['a', 'b', 'c'], range(2)]) a = pd.DataFrame(0.0, index=range(3),columns=columns, dtype='float') b = pd.Series([13.0, 15.0]) import pdb pdb.set_trace() a.loc[1,'b'] = b
and just stepping it to the "high level" and finding a problem arises in
if isinstance(value, ABCSeries): value = self._align_series(indexer, value)
and then again going over it (with a finer jagged comb) with a breakpoint starting at the line calling self._align_series(indexer, value) .
Please note that if you change the index b as well as MultiIndex:
b = pd.Series([13.0, 15.0], index=pd.MultiIndex.from_product([['b'], [0,1]]))
then
import pandas as pd columns = pd.MultiIndex.from_product([['a', 'b', 'c'], range(2)]) a = pd.DataFrame(0.0, index=range(3),columns=columns, dtype='float') b = pd.Series([13.0, 15.0], index=pd.MultiIndex.from_product([['b'], [0,1]])) a.loc[1,'b'] = b print(a)
gives
abc 0 1 0 1 0 1 0 0 0 0 0 0 0 1 0 0 13 15 0 0 2 0 0 0 0 0 0