I have a 3D panel data. I am unable to reindex it to a multi index along level 2.
I have created the multi index 'mind'.
import pandas as pd
mind = pd.MultiIndex.from_arrays([['Consumer,Cyclical','Industrial','Software'], ['Retail','MiscellaneousManufactur','Technology'], ['AZO','AZZ','AZPN']],names=['sec','sub','ticker'])
dfclose = pd.DataFrame([[1.1,2.1,3.1],[1.2,2.2,3.2]], index=['2013-09-02','2013-09-03'], columns=['AZO','AZZ','AZPN'])
dfmean = dfclose - dfclose.mean()
pdata2 = pd.Panel({'close':dfclose, 'mean':dfmean})
pdata2.minor_axis.name='ticker'
pdata3=pdata2.reindex_axis(mind,axis=2,level='ticker')
But the pdata3 is not getting mapped to the new multi index and giving NaN.
This appears to be a bug in 0.12 (and will be fixed in 0.13).
A workaround is not to reindex after, but to use the MultiIndex when creating dfclose:
dfclose = pd.DataFrame([[1.1, 2.1, 3.1], [1.2, 2.2, 3.2]],
index=['2013-09-02','2013-09-03'],
columns=mind)
dfmean = dfclose - dfclose.mean()
pdata2 = pd.Panel({'close':dfclose, 'mean':dfmean})
pdata2.minor_axis.name='ticker'
In [11]: pdata2.iloc[0]
Out[12]:
sec Consumer,Cyclical Industrial Software
sub Retail MiscellaneousManufactur Technology
ticker AZO AZZ AZPN
2013-09-02 1.1 2.1 3.1
2013-09-03 1.2 2.2 3.2
Another option is to just use a DataFrame:
In [12]: pd.concat([dfmean, dfclose], axis=1, keys=['dfmean' ,'dfclose'])
Out[12]:
dfmean dfclose
sec Consumer,Cyclical Industrial Software Consumer,Cyclical Industrial Software
sub Retail MiscellaneousManufactur Technology Retail MiscellaneousManufactur Technology
ticker AZO AZZ AZPN AZO AZZ AZPN
2013-09-02 -0.05 -0.05 -0.05 1.1 2.1 3.1
2013-09-03 0.05 0.05 0.05 1.2 2.2 3.2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.