Rob
Rob

Reputation: 3513

Indexing a pandas.Panel counterintuitive or a bug?

I am having some trouble understanding the indexing of a pandas.Panel (3D data structure). In the documentation it is stated that the indexing works as:

Getting values from an object with multi-axes selection uses the following notation (using .loc as an example, but applies to .iloc and .ix as well). Any of the axes accessors may be the null slice :. Axes left out of the specification are assumed to be :. (e.g. p.loc['a'] is equiv to p.loc['a', :, :])

p.loc[item_indexer,major_indexer,minor_indexer]

Now i would assume that the order of the remaining indices do not change when a DataFrame is extracted, but:

from pandas import Panel
from numpy import arange
p = Panel(arange(24).reshape(2,3,4))
p.shape
Out[4]: (2, 3, 4)
p.iloc[0].shape # original order
Out[5]: (3, 4)
p.iloc[:,0].shape # transposed
Out[6]: (4, 2)
p.iloc[:,:,0].shape # transposed
Out[7]: (3, 2)
p.iloc[:,0,:].shape # transpose (same as [6])
Out[8]: (4, 2)
p.iloc[1:,0,:].shape # Slicing item_indexer, then transpose
Out[9]: (4, 1)
p.iloc[1:,0].shape # Expected to get the same as [9], but slicing minor_indexer instead????
Out[10]: (3, 2)

Any ideas why the DataFrame is transposed when indexing the major_index or minor_index, but not the item_index? And why is the last example different from the one before?

Link to github issue

Upvotes: 2

Views: 267

Answers (1)

Rob
Rob

Reputation: 3513

Recently there has been some discussion in the github issue. It seems that pandas is not very well geared towards N-dimensional data (N >= 3). There is an alternative module xray, which is like pandas but for ND data. Otherwise, you can use plain pandas (2D) DataFrames with MultiIndex to simulate ND data.

Upvotes: 1

Related Questions