My original question was how to add columns in all the sublevels of a multindex column. This solution worked fine for something like a rolling mean, or a difference versus a shifted value.
But it refuses to work when applied to math on same-dimension cross-sections of different columns. It returns all nan.
My hypothesis is that pandas is unhappy because the columns are named differently, so it cannot match them to subtract? So I should do some on-the-fly renaming (seems gross) or does this mean I'm missing something more fundamental about this operation.
index = pd.DatetimeIndex(start='2018-1-1',periods=5,freq="M")
persons = ['mike', 'dave', 'matt']
measures = ['spin', 'drag', 'bezel']
cols = pd.MultiIndex.from_product([persons, measures],names=['human', 'measure'])
xf = pd.DataFrame(index=index, data=np.random.rand(5,9), columns=cols)
idx = pd.IndexSlice
#this shows that both cross sections have data
print(xf.xs('spin', axis=1, level=1, drop_level=False))
print(xf.xs('drag', axis=1, level=1, drop_level=False))
#this works fine.
zf = xf.xs('spin', axis=1, level=1, drop_level=False) - xf.xs('spin', axis=1, level=1, drop_level=False).shift(1)
#but this returns all NaN
qf = xf.xs('spin', axis=1, level=1, drop_level=False)+xf.xs('drag', axis=1, level=1, drop_level=False)
zf
Problem is data alignment - need same values of
MultiIndex
, else getNaN
s:So if remove
drop_level=False
then columns are same, but is necessary createMultiIndex
: