4 Panel4D (Experimental)

Warning

In 0.19.0 Panel4D is deprecated and will be removed in a future version. The recommended way to represent these types of n-dimensional data are with the xarray package. Pandas provides a to_xarray() method to automate this conversion.

Panel4D is a 4-Dimensional named container very much like a Panel, but having 4 named dimensions. It is intended as a test bed for more N-Dimensional named containers.

  • labels: axis 0, each item corresponds to a Panel contained inside
  • items: axis 1, each item corresponds to a DataFrame contained inside
  • major_axis: axis 2, it is the index (rows) of each of the DataFrames
  • minor_axis: axis 3, it is the columns of each of the DataFrames

Panel4D is a sub-class of Panel, so most methods that work on Panels are applicable to Panel4D. The following methods are disabled:

  • join , to_frame , to_excel , to_sparse , groupby

Construction of Panel4D works in a very similar manner to a Panel

4.1 From 4D ndarray with optional axis labels

In [1]: p4d = pd.Panel4D(np.random.randn(2, 2, 5, 4),
   ...:                  labels=['Label1','Label2'],
   ...:                  items=['Item1', 'Item2'],
   ...:                  major_axis=pd.date_range('1/1/2000', periods=5),
   ...:                  minor_axis=['A', 'B', 'C', 'D'])
   ...: 

In [2]: p4d
Out[2]: 
<class 'pandas.core.panelnd.Panel4D'>
Dimensions: 2 (labels) x 2 (items) x 5 (major_axis) x 4 (minor_axis)
Labels axis: Label1 to Label2
Items axis: Item1 to Item2
Major_axis axis: 2000-01-01 00:00:00 to 2000-01-05 00:00:00
Minor_axis axis: A to D

4.2 From dict of Panel objects

In [3]: data = { 'Label1' : pd.Panel({ 'Item1' : pd.DataFrame(np.random.randn(4, 3)) }),
   ...:          'Label2' : pd.Panel({ 'Item2' : pd.DataFrame(np.random.randn(4, 2)) }) }
   ...: 

In [4]: pd.Panel4D(data)
Out[4]: 
<class 'pandas.core.panelnd.Panel4D'>
Dimensions: 2 (labels) x 2 (items) x 4 (major_axis) x 3 (minor_axis)
Labels axis: Label1 to Label2
Items axis: Item1 to Item2
Major_axis axis: 0 to 3
Minor_axis axis: 0 to 2

Note that the values in the dict need only be convertible to Panels. Thus, they can be any of the other valid inputs to Panel as per above.

4.3 Slicing

Slicing works in a similar manner to a Panel. [] slices the first dimension. .ix allows you to slice arbitrarily and get back lower dimensional objects

In [5]: p4d['Label1']
Out[5]: 
<class 'pandas.core.panel.Panel'>
Dimensions: 2 (items) x 5 (major_axis) x 4 (minor_axis)
Items axis: Item1 to Item2
Major_axis axis: 2000-01-01 00:00:00 to 2000-01-05 00:00:00
Minor_axis axis: A to D

4D -> Panel

In [6]: p4d.ix[:,:,:,'A']
Out[6]: 
<class 'pandas.core.panel.Panel'>
Dimensions: 2 (items) x 2 (major_axis) x 5 (minor_axis)
Items axis: Label1 to Label2
Major_axis axis: Item1 to Item2
Minor_axis axis: 2000-01-01 00:00:00 to 2000-01-05 00:00:00

4D -> DataFrame

In [7]: p4d.ix[:,:,0,'A']
Out[7]: 
       Label1  Label2
Item1 -0.3743  1.6851
Item2  0.0413 -0.0593

4D -> Series

In [8]: p4d.ix[:,0,0,'A']
Out[8]: 
Label1   -0.3743
Label2    1.6851
Name: A, dtype: float64

4.4 Transposing

A Panel4D can be rearranged using its transpose method (which does not make a copy by default unless the data are heterogeneous):

In [9]: p4d.transpose(3, 2, 1, 0)
Out[9]: 
<class 'pandas.core.panelnd.Panel4D'>
Dimensions: 4 (labels) x 5 (items) x 2 (major_axis) x 2 (minor_axis)
Labels axis: A to D
Items axis: 2000-01-01 00:00:00 to 2000-01-05 00:00:00
Major_axis axis: Item1 to Item2
Minor_axis axis: Label1 to Label2