Code Sample, a copy-pastable example if possible
Problem description
I find a bug when using panel.apply(), and with some digging, I find an interesting indexing problem caused by multiindex in Panel Object. It is demonstrated below:
#
import pandas as pd
pn1 = pd.Panel(pd.np.random.random(size=(3,4,5)),items=['AAA','BBB','CCC'])
print pn1.loc[:,:,1]
pn2 = pn1.copy()
pn2.major_axis = pd.MultiIndex.from_product([list('AB'),list('ab')])
print pn2.loc[:,:,1]
Output:
AAA BBB CCC
0 0.182982 0.774812 0.894589
1 0.654716 0.695453 0.790078
2 0.340826 0.941283 0.938544
3 0.351851 0.971896 0.362317
A B
a b a b
AAA 0.182982 0.654716 0.340826 0.351851
BBB 0.774812 0.695453 0.941283 0.971896
CCC 0.894589 0.790078 0.938544 0.362317
With a multiindex as the major_axis of Panel object, the indexing result is transposed. And because of that, when using pn.apply(func, axis=('items','major'))
, it give you the wrong results when the major_axis is a multiindex.
Is this a bug? if not, then what is the purpose that it is designed like this?
Thanks for help.
Comment From: jreback
duplicate of https://github.com/pandas-dev/pandas/issues/6286
Panel
is being deprecated, mainly because of the difficulties of indexing :>
see https://github.com/pandas-dev/pandas/issues/13563
for multi-dimensional data would recommend a DataFrame
with a multi-index or use https://github.com/pydata/xarray