pandas 0.11.0.dev-9988e5f documentation

Intro to Data Structures

We’ll start with a quick, non-comprehensive overview of the fundamental data structures in pandas to get you started. The fundamental behavior about data types, indexing, and axis labeling / alignment apply across all of the objects. To get started, import numpy and load pandas into your namespace:

In [305]: import numpy as np

# will use a lot in examples
In [306]: randn = np.random.randn

In [307]: from pandas import *

Here is a basic tenet to keep in mind: data alignment is intrinsic. The link between labels and data will not be broken unless done so explicitly by you.

We’ll give a brief intro to the data structures, then consider all of the broad categories of functionality and methods in separate sections.

When using pandas, we recommend the following import convention:

import pandas as pd

Series

Series is a one-dimensional labeled array (technically a subclass of ndarray) capable of holding any data type (integers, strings, floating point numbers, Python objects, etc.). The axis labels are collectively referred to as the index. The basic method to create a Series is to call:

>>> s = Series(data, index=index)

Here, data can be many different things:

  • a Python dict
  • an ndarray
  • a scalar value (like 5)

The passed index is a list of axis labels. Thus, this separates into a few cases depending on what data is:

From ndarray

If data is an ndarray, index must be the same length as data. If no index is passed, one will be created having values [0, ..., len(data) - 1].

In [308]: s = Series(randn(5), index=['a', 'b', 'c', 'd', 'e'])

In [309]: s
Out[309]: 
a    0.314
b   -0.002
c    0.072
d    0.893
e    0.681
dtype: float64

In [310]: s.index
Out[310]: Index([a, b, c, d, e], dtype=object)

In [311]: Series(randn(5))
Out[311]: 
0   -0.340
1    0.215
2   -0.078
3   -0.178
4    0.491
dtype: float64

Note

Starting in v0.8.0, pandas supports non-unique index values. In previous version, if the index values are not unique an exception will not be raised immediately, but attempting any operation involving the index will later result in an exception. In other words, the Index object containing the labels “lazily” checks whether the values are unique. The reason for being lazy is nearly all performance-based (there are many instances in computations, like parts of GroupBy, where the index is not used).

From dict

If data is a dict, if index is passed the values in data corresponding to the labels in the index will be pulled out. Otherwise, an index will be constructed from the sorted keys of the dict, if possible.

In [312]: d = {'a' : 0., 'b' : 1., 'c' : 2.}

In [313]: Series(d)
Out[313]: 
a    0
b    1
c    2
dtype: float64

In [314]: Series(d, index=['b', 'c', 'd', 'a'])
Out[314]: 
b     1
c     2
d   NaN
a     0
dtype: float64

Note

NaN (not a number) is the standard missing data marker used in pandas

From scalar value If data is a scalar value, an index must be provided. The value will be repeated to match the length of index

In [315]: Series(5., index=['a', 'b', 'c', 'd', 'e'])
Out[315]: 
a    5
b    5
c    5
d    5
e    5
dtype: float64

Series is ndarray-like

As a subclass of ndarray, Series is a valid argument to most NumPy functions and behaves similarly to a NumPy array. However, things like slicing also slice the index.

In [316]: s[0]
Out[316]: 0.31422552353417077

In [317]: s[:3]
Out[317]: 
a    0.314
b   -0.002
c    0.072
dtype: float64

In [318]: s[s > s.median()]
Out[318]: 
d    0.893
e    0.681
dtype: float64

In [319]: s[[4, 3, 1]]
Out[319]: 
e    0.681
d    0.893
b   -0.002
dtype: float64

In [320]: np.exp(s)
Out[320]: 
a    1.369
b    0.998
c    1.074
d    2.441
e    1.975
dtype: float64

We will address array-based indexing in a separate section.

Series is dict-like

A Series is like a fixed-size dict in that you can get and set values by index label:

In [321]: s['a']
Out[321]: 0.31422552353417077

In [322]: s['e'] = 12.

In [323]: s
Out[323]: 
a     0.314
b    -0.002
c     0.072
d     0.893
e    12.000
dtype: float64

In [324]: 'e' in s
Out[324]: True

In [325]: 'f' in s
Out[325]: False

If a label is not contained, an exception is raised:

>>> s['f']
KeyError: 'f'

Using the get method, a missing label will return None or specified default:

In [326]: s.get('f')

In [327]: s.get('f', np.nan)
Out[327]: nan

Vectorized operations and label alignment with Series

When doing data analysis, as with raw NumPy arrays looping through Series value-by-value is usually not necessary. Series can be also be passed into most NumPy methods expecting an ndarray.

In [328]: s + s
Out[328]: 
a     0.628
b    -0.003
c     0.144
d     1.785
e    24.000
dtype: float64

In [329]: s * 2
Out[329]: 
a     0.628
b    -0.003
c     0.144
d     1.785
e    24.000
dtype: float64

In [330]: np.exp(s)
Out[330]: 
a         1.369
b         0.998
c         1.074
d         2.441
e    162754.791
dtype: float64

A key difference between Series and ndarray is that operations between Series automatically align the data based on label. Thus, you can write computations without giving consideration to whether the Series involved have the same labels.

In [331]: s[1:] + s[:-1]
Out[331]: 
a      NaN
b   -0.003
c    0.144
d    1.785
e      NaN
dtype: float64

The result of an operation between unaligned Series will have the union of the indexes involved. If a label is not found in one Series or the other, the result will be marked as missing (NaN). Being able to write code without doing any explicit data alignment grants immense freedom and flexibility in interactive data analysis and research. The integrated data alignment features of the pandas data structures set pandas apart from the majority of related tools for working with labeled data.

Note

In general, we chose to make the default result of operations between differently indexed objects yield the union of the indexes in order to avoid loss of information. Having an index label, though the data is missing, is typically important information as part of a computation. You of course have the option of dropping labels with missing data via the dropna function.

Name attribute

Series can also have a name attribute:

In [332]: s = Series(np.random.randn(5), name='something')

In [333]: s
Out[333]: 
0   -1.360
1    1.592
2    1.007
3    0.698
4   -1.891
Name: something, dtype: float64

In [334]: s.name
Out[334]: 'something'

The Series name will be assigned automatically in many cases, in particular when taking 1D slices of DataFrame as you will see below.

DataFrame

DataFrame is a 2-dimensional labeled data structure with columns of potentially different types. You can think of it like a spreadsheet or SQL table, or a dict of Series objects. It is generally the most commonly used pandas object. Like Series, DataFrame accepts many different kinds of input:

  • Dict of 1D ndarrays, lists, dicts, or Series
  • 2-D numpy.ndarray
  • Structured or record ndarray
  • A Series
  • Another DataFrame

Along with the data, you can optionally pass index (row labels) and columns (column labels) arguments. If you pass an index and / or columns, you are guaranteeing the index and / or columns of the resulting DataFrame. Thus, a dict of Series plus a specific index will discard all data not matching up to the passed index.

If axis labels are not passed, they will be constructed from the input data based on common sense rules.

From dict of Series or dicts

The result index will be the union of the indexes of the various Series. If there are any nested dicts, these will be first converted to Series. If no columns are passed, the columns will be the sorted list of dict keys.

In [335]: d = {'one' : Series([1., 2., 3.], index=['a', 'b', 'c']),
   .....:      'two' : Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}
   .....:

In [336]: df = DataFrame(d)

In [337]: df
Out[337]: 
   one  two
a    1    1
b    2    2
c    3    3
d  NaN    4

In [338]: DataFrame(d, index=['d', 'b', 'a'])
Out[338]: 
   one  two
d  NaN    4
b    2    2
a    1    1

In [339]: DataFrame(d, index=['d', 'b', 'a'], columns=['two', 'three'])
Out[339]: 
   two three
d    4   NaN
b    2   NaN
a    1   NaN

The row and column labels can be accessed respectively by accessing the index and columns attributes:

Note

When a particular set of columns is passed along with a dict of data, the passed columns override the keys in the dict.

In [340]: df.index
Out[340]: Index([a, b, c, d], dtype=object)

In [341]: df.columns
Out[341]: Index([one, two], dtype=object)

From dict of ndarrays / lists

The ndarrays must all be the same length. If an index is passed, it must clearly also be the same length as the arrays. If no index is passed, the result will be range(n), where n is the array length.

In [342]: d = {'one' : [1., 2., 3., 4.],
   .....:      'two' : [4., 3., 2., 1.]}
   .....:

In [343]: DataFrame(d)
Out[343]: 
   one  two
0    1    4
1    2    3
2    3    2
3    4    1

In [344]: DataFrame(d, index=['a', 'b', 'c', 'd'])
Out[344]: 
   one  two
a    1    4
b    2    3
c    3    2
d    4    1

From structured or record array

This case is handled identically to a dict of arrays.

In [345]: data = np.zeros((2,),dtype=[('A', 'i4'),('B', 'f4'),('C', 'a10')])

In [346]: data[:] = [(1,2.,'Hello'),(2,3.,"World")]

In [347]: DataFrame(data)
Out[347]: 
   A  B      C
0  1  2  Hello
1  2  3  World

In [348]: DataFrame(data, index=['first', 'second'])
Out[348]: 
        A  B      C
first   1  2  Hello
second  2  3  World

In [349]: DataFrame(data, columns=['C', 'A', 'B'])
Out[349]: 
       C  A  B
0  Hello  1  2
1  World  2  3

Note

DataFrame is not intended to work exactly like a 2-dimensional NumPy ndarray.

From a list of dicts

In [350]: data2 = [{'a': 1, 'b': 2}, {'a': 5, 'b': 10, 'c': 20}]

In [351]: DataFrame(data2)
Out[351]: 
   a   b   c
0  1   2 NaN
1  5  10  20

In [352]: DataFrame(data2, index=['first', 'second'])
Out[352]: 
        a   b   c
first   1   2 NaN
second  5  10  20

In [353]: DataFrame(data2, columns=['a', 'b'])
Out[353]: 
   a   b
0  1   2
1  5  10

From a Series

The result will be a DataFrame with the same index as the input Series, and with one column whose name is the original name of the Series (only if no other column name provided).

Missing Data

Much more will be said on this topic in the Missing data section. To construct a DataFrame with missing data, use np.nan for those values which are missing. Alternatively, you may pass a numpy.MaskedArray as the data argument to the DataFrame constructor, and its masked entries will be considered missing.

Alternate Constructors

DataFrame.from_dict

DataFrame.from_dict takes a dict of dicts or a dict of array-like sequences and returns a DataFrame. It operates like the DataFrame constructor except for the orient parameter which is 'columns' by default, but which can be set to 'index' in order to use the dict keys as row labels.

DataFrame.from_records

DataFrame.from_records takes a list of tuples or an ndarray with structured dtype. Works analogously to the normal DataFrame constructor, except that index maybe be a specific field of the structured dtype to use as the index. For example:

In [354]: data
Out[354]: 
array([(1, 2.0, 'Hello'), (2, 3.0, 'World')], 
      dtype=[('A', '<i4'), ('B', '<f4'), ('C', '|S10')])

In [355]: DataFrame.from_records(data, index='C')
Out[355]: 
       A  B
C          
Hello  1  2
World  2  3

DataFrame.from_items

DataFrame.from_items works analogously to the form of the dict constructor that takes a sequence of (key, value) pairs, where the keys are column (or row, in the case of orient='index') names, and the value are the column values (or row values). This can be useful for constructing a DataFrame with the columns in a particular order without having to pass an explicit list of columns:

In [356]: DataFrame.from_items([('A', [1, 2, 3]), ('B', [4, 5, 6])])
Out[356]: 
   A  B
0  1  4
1  2  5
2  3  6

If you pass orient='index', the keys will be the row labels. But in this case you must also pass the desired column names:

In [357]: DataFrame.from_items([('A', [1, 2, 3]), ('B', [4, 5, 6])],
   .....:                      orient='index', columns=['one', 'two', 'three'])
   .....:
Out[357]: 
   one  two  three
A    1    2      3
B    4    5      6

Column selection, addition, deletion

You can treat a DataFrame semantically like a dict of like-indexed Series objects. Getting, setting, and deleting columns works with the same syntax as the analogous dict operations:

In [358]: df['one']
Out[358]: 
a     1
b     2
c     3
d   NaN
Name: one, dtype: float64

In [359]: df['three'] = df['one'] * df['two']

In [360]: df['flag'] = df['one'] > 2

In [361]: df
Out[361]: 
   one  two  three   flag
a    1    1      1  False
b    2    2      4  False
c    3    3      9   True
d  NaN    4    NaN  False

Columns can be deleted or popped like with a dict:

In [362]: del df['two']

In [363]: three = df.pop('three')

In [364]: df
Out[364]: 
   one   flag
a    1  False
b    2  False
c    3   True
d  NaN  False

When inserting a scalar value, it will naturally be propagated to fill the column:

In [365]: df['foo'] = 'bar'

In [366]: df
Out[366]: 
   one   flag  foo
a    1  False  bar
b    2  False  bar
c    3   True  bar
d  NaN  False  bar

When inserting a Series that does not have the same index as the DataFrame, it will be conformed to the DataFrame’s index:

In [367]: df['one_trunc'] = df['one'][:2]

In [368]: df
Out[368]: 
   one   flag  foo  one_trunc
a    1  False  bar          1
b    2  False  bar          2
c    3   True  bar        NaN
d  NaN  False  bar        NaN

You can insert raw ndarrays but their length must match the length of the DataFrame’s index.

By default, columns get inserted at the end. The insert function is available to insert at a particular location in the columns:

In [369]: df.insert(1, 'bar', df['one'])

In [370]: df
Out[370]: 
   one  bar   flag  foo  one_trunc
a    1    1  False  bar          1
b    2    2  False  bar          2
c    3    3   True  bar        NaN
d  NaN  NaN  False  bar        NaN

Indexing / Selection

The basics of indexing are as follows:

Operation Syntax Result
Select column df[col] Series
Select row by label df.xs(label) or df.ix[label] Series
Select row by location (int) df.ix[loc] Series
Slice rows df[5:10] DataFrame
Select rows by boolean vector df[bool_vec] DataFrame

Row selection, for example, returns a Series whose index is the columns of the DataFrame:

In [371]: df.xs('b')
Out[371]: 
one              2
bar              2
flag         False
foo            bar
one_trunc        2
Name: b, dtype: object

In [372]: df.ix[2]
Out[372]: 
one             3
bar             3
flag         True
foo           bar
one_trunc     NaN
Name: c, dtype: object

Note if a DataFrame contains columns of multiple dtypes, the dtype of the row will be chosen to accommodate all of the data types (dtype=object is the most general).

For a more exhaustive treatment of more sophisticated label-based indexing and slicing, see the section on indexing. We will address the fundamentals of reindexing / conforming to new sets of lables in the section on reindexing.

Data alignment and arithmetic

Data alignment between DataFrame objects automatically align on both the columns and the index (row labels). Again, the resulting object will have the union of the column and row labels.

In [373]: df = DataFrame(randn(10, 4), columns=['A', 'B', 'C', 'D'])

In [374]: df2 = DataFrame(randn(7, 3), columns=['A', 'B', 'C'])

In [375]: df + df2
Out[375]: 
       A      B      C   D
0  0.229  1.547 -1.499 NaN
1  0.121 -0.234 -0.705 NaN
2 -0.561 -1.550  0.643 NaN
3 -0.263  1.071 -0.060 NaN
4 -2.588 -0.752 -1.227 NaN
5  0.628 -0.095 -3.236 NaN
6  0.983 -0.823 -0.720 NaN
7    NaN    NaN    NaN NaN
8    NaN    NaN    NaN NaN
9    NaN    NaN    NaN NaN

When doing an operation between DataFrame and Series, the default behavior is to align the Series index on the DataFrame columns, thus broadcasting row-wise. For example:

In [376]: df - df.ix[0]
Out[376]: 
       A      B      C      D
0  0.000  0.000  0.000  0.000
1  0.879 -2.485  0.133 -0.958
2  0.246 -1.482  0.106  0.685
3  0.482 -1.571  0.099 -0.054
4 -0.511 -1.333 -0.217  0.772
5  0.114 -1.401 -1.682  0.386
6  1.131 -1.280  0.060 -0.113
7 -0.313 -3.004  1.531  0.829
8 -0.232 -1.702 -0.982 -0.460
9  0.113 -1.353 -0.456  0.598

In the special case of working with time series data, if the Series is a TimeSeries (which it will be automatically if the index contains datetime objects), and the DataFrame index also contains dates, the broadcasting will be column-wise:

In [377]: index = date_range('1/1/2000', periods=8)

In [378]: df = DataFrame(randn(8, 3), index=index,
   .....:                columns=['A', 'B', 'C'])
   .....:

In [379]: df
Out[379]: 
                A      B      C
2000-01-01  0.302  1.113 -0.543
2000-01-02 -2.696  0.431 -0.431
2000-01-03  1.667  0.717 -0.920
2000-01-04 -0.025  0.069  0.602
2000-01-05  0.867  0.093 -2.607
2000-01-06  0.309 -0.548 -2.045
2000-01-07 -1.666 -1.440  1.326
2000-01-08  0.222  1.841  1.165

In [380]: type(df['A'])
Out[380]: pandas.core.series.TimeSeries

In [381]: df - df['A']
Out[381]: 
            A      B      C
2000-01-01  0  0.811 -0.845
2000-01-02  0  3.127  2.264
2000-01-03  0 -0.950 -2.586
2000-01-04  0  0.094  0.626
2000-01-05  0 -0.774 -3.474
2000-01-06  0 -0.858 -2.354
2000-01-07  0  0.226  2.993
2000-01-08  0  1.619  0.943

Technical purity aside, this case is so common in practice that supporting the special case is preferable to the alternative of forcing the user to transpose and do column-based alignment like so:

In [382]: (df.T - df['A']).T
Out[382]: 
            A      B      C
2000-01-01  0  0.811 -0.845
2000-01-02  0  3.127  2.264
2000-01-03  0 -0.950 -2.586
2000-01-04  0  0.094  0.626
2000-01-05  0 -0.774 -3.474
2000-01-06  0 -0.858 -2.354
2000-01-07  0  0.226  2.993
2000-01-08  0  1.619  0.943

For explicit control over the matching and broadcasting behavior, see the section on flexible binary operations.

Operations with scalars are just as you would expect:

In [383]: df * 5 + 2
Out[383]: 
                 A       B       C
2000-01-01   3.510   7.563  -0.714
2000-01-02 -11.478   4.156  -0.155
2000-01-03  10.333   5.583  -2.599
2000-01-04   1.877   2.345   5.009
2000-01-05   6.333   2.465 -11.034
2000-01-06   3.547  -0.742  -8.224
2000-01-07  -6.331  -5.199   8.632
2000-01-08   3.108  11.205   7.826

In [384]: 1 / df
Out[384]: 
                 A       B      C
2000-01-01   3.312   0.899 -1.842
2000-01-02  -0.371   2.319 -2.320
2000-01-03   0.600   1.395 -1.087
2000-01-04 -40.659  14.493  1.662
2000-01-05   1.154  10.763 -0.384
2000-01-06   3.233  -1.823 -0.489
2000-01-07  -0.600  -0.695  0.754
2000-01-08   4.511   0.543  0.858

In [385]: df ** 4
Out[385]: 
                    A          B       C
2000-01-01  8.312e-03  1.532e+00   0.087
2000-01-02  5.279e+01  3.460e-02   0.035
2000-01-03  7.715e+00  2.638e-01   0.716
2000-01-04  3.659e-07  2.266e-05   0.131
2000-01-05  5.640e-01  7.452e-05  46.184
2000-01-06  9.152e-03  9.045e-02  17.482
2000-01-07  7.709e+00  4.297e+00   3.095
2000-01-08  2.415e-03  1.149e+01   1.843

Boolean operators work as well:

In [386]: df1 = DataFrame({'a' : [1, 0, 1], 'b' : [0, 1, 1] }, dtype=bool)

In [387]: df2 = DataFrame({'a' : [0, 1, 1], 'b' : [1, 1, 0] }, dtype=bool)

In [388]: df1 & df2
Out[388]: 
       a      b
0  False  False
1  False   True
2   True  False

In [389]: df1 | df2
Out[389]: 
      a     b
0  True  True
1  True  True
2  True  True

In [390]: df1 ^ df2
Out[390]: 
       a      b
0   True   True
1   True  False
2  False   True

In [391]: -df1
Out[391]: 
       a      b
0  False   True
1   True  False
2  False  False

Transposing

To transpose, access the T attribute (also the transpose function), similar to an ndarray:

# only show the first 5 rows
In [392]: df[:5].T
Out[392]: 
   2000-01-01  2000-01-02  2000-01-03  2000-01-04  2000-01-05
A       0.302      -2.696       1.667      -0.025       0.867
B       1.113       0.431       0.717       0.069       0.093
C      -0.543      -0.431      -0.920       0.602      -2.607

DataFrame interoperability with NumPy functions

Elementwise NumPy ufuncs (log, exp, sqrt, ...) and various other NumPy functions can be used with no issues on DataFrame, assuming the data within are numeric:

In [393]: np.exp(df)
Out[393]: 
                A      B      C
2000-01-01  1.352  3.042  0.581
2000-01-02  0.068  1.539  0.650
2000-01-03  5.294  2.048  0.399
2000-01-04  0.976  1.071  1.825
2000-01-05  2.379  1.097  0.074
2000-01-06  1.362  0.578  0.129
2000-01-07  0.189  0.237  3.767
2000-01-08  1.248  6.303  3.206

In [394]: np.asarray(df)
Out[394]: 
array([[ 0.3019,  1.1125, -0.5428],
       [-2.6955,  0.4313, -0.4311],
       [ 1.6666,  0.7167, -0.9197],
       [-0.0246,  0.069 ,  0.6018],
       [ 0.8666,  0.0929, -2.6069],
       [ 0.3093, -0.5484, -2.0448],
       [-1.6663, -1.4398,  1.3264],
       [ 0.2217,  1.841 ,  1.1651]])

The dot method on DataFrame implements matrix multiplication:

In [395]: df.T.dot(df)
Out[395]: 
        A      B       C
A  13.808  3.084  -5.393
B   3.084  7.714  -0.293
C  -5.393 -0.293  15.782

Similarly, the dot method on Series implements dot product:

In [396]: s1 = Series(np.arange(5,10))

In [397]: s1.dot(s1)
Out[397]: 255

DataFrame is not intended to be a drop-in replacement for ndarray as its indexing semantics are quite different in places from a matrix.

Console display

For very large DataFrame objects, only a summary will be printed to the console (here I am reading a CSV version of the baseball dataset from the plyr R package):

In [398]: baseball = read_csv('data/baseball.csv')

In [399]: print baseball
<class 'pandas.core.frame.DataFrame'>
Int64Index: 100 entries, 88641 to 89534
Data columns:
id       100  non-null values
year     100  non-null values
stint    100  non-null values
team     100  non-null values
lg       100  non-null values
g        100  non-null values
ab       100  non-null values
r        100  non-null values
h        100  non-null values
X2b      100  non-null values
X3b      100  non-null values
hr       100  non-null values
rbi      100  non-null values
sb       100  non-null values
cs       100  non-null values
bb       100  non-null values
so       100  non-null values
ibb      100  non-null values
hbp      100  non-null values
sh       100  non-null values
sf       100  non-null values
gidp     100  non-null values
dtypes: float64(9), int64(10), object(3)

However, using to_string will return a string representation of the DataFrame in tabular form, though it won’t always fit the console width:

In [400]: print baseball.ix[-20:, :12].to_string()
              id  year  stint team  lg    g   ab    r    h  X2b  X3b  hr
88641  womacto01  2006      2  CHN  NL   19   50    6   14    1    0   1
88643  schilcu01  2006      1  BOS  AL   31    2    0    1    0    0   0
88645  myersmi01  2006      1  NYA  AL   62    0    0    0    0    0   0
88649  helliri01  2006      1  MIL  NL   20    3    0    0    0    0   0
88650  johnsra05  2006      1  NYA  AL   33    6    0    1    0    0   0
88652  finlest01  2006      1  SFN  NL  139  426   66  105   21   12   6
88653  gonzalu01  2006      1  ARI  NL  153  586   93  159   52    2  15
88662   seleaa01  2006      1  LAN  NL   28   26    2    5    1    0   0
89177  francju01  2007      2  ATL  NL   15   40    1   10    3    0   0
89178  francju01  2007      1  NYN  NL   40   50    7   10    0    0   1
89330   zaungr01  2007      1  TOR  AL  110  331   43   80   24    1  10
89333  witasja01  2007      1  TBA  AL    3    0    0    0    0    0   0
89334  williwo02  2007      1  HOU  NL   33   59    3    6    0    0   1
89335  wickmbo01  2007      2  ARI  NL    8    0    0    0    0    0   0
89336  wickmbo01  2007      1  ATL  NL   47    0    0    0    0    0   0
89337  whitero02  2007      1  MIN  AL   38  109    8   19    4    0   4
89338  whiteri01  2007      1  HOU  NL   20    1    0    0    0    0   0
89339  wellsda01  2007      2  LAN  NL    7   15    2    4    1    0   0
89340  wellsda01  2007      1  SDN  NL   22   38    1    4    0    0   0
89341  weathda01  2007      1  CIN  NL   67    0    0    0    0    0   0
89343  walketo04  2007      1  OAK  AL   18   48    5   13    1    0   0
89345  wakefti01  2007      1  BOS  AL    1    2    0    0    0    0   0
89347  vizquom01  2007      1  SFN  NL  145  513   54  126   18    3   4
89348  villoro01  2007      1  NYA  AL    6    0    0    0    0    0   0
89352  valenjo03  2007      1  NYN  NL   51  166   18   40   11    1   3
89354  trachst01  2007      2  CHN  NL    4    7    0    1    0    0   0
89355  trachst01  2007      1  BAL  AL    3    5    0    0    0    0   0
89359  timlimi01  2007      1  BOS  AL    4    0    0    0    0    0   0
89360  thomeji01  2007      1  CHA  AL  130  432   79  119   19    0  35
89361  thomafr04  2007      1  TOR  AL  155  531   63  147   30    0  26
89363  tavarju01  2007      1  BOS  AL    2    4    0    1    0    0   0
89365  sweenma01  2007      2  LAN  NL   30   33    2    9    1    0   0
89366  sweenma01  2007      1  SFN  NL   76   90   18   23    8    0   2
89367  suppaje01  2007      1  MIL  NL   33   61    4    8    0    0   0
89368  stinnke01  2007      1  SLN  NL   26   82    7   13    3    0   1
89370  stantmi02  2007      1  CIN  NL   67    2    0    0    0    0   0
89371  stairma01  2007      1  TOR  AL  125  357   58  103   28    1  21
89372  sprinru01  2007      1  SLN  NL   72    1    0    0    0    0   0
89374   sosasa01  2007      1  TEX  AL  114  412   53  104   24    1  21
89375  smoltjo01  2007      1  ATL  NL   30   54    1    5    1    0   0
89378  sheffga01  2007      1  DET  AL  133  494  107  131   20    1  25
89381   seleaa01  2007      1  NYN  NL   31    4    0    0    0    0   0
89382  seaneru01  2007      1  LAN  NL   68    1    0    0    0    0   0
89383  schmija01  2007      1  LAN  NL    6    7    1    1    0    0   1
89384  schilcu01  2007      1  BOS  AL    1    2    0    1    0    0   0
89385  sandere02  2007      1  KCA  AL   24   73   12   23    7    0   2
89388  rogerke01  2007      1  DET  AL    1    2    0    0    0    0   0
89389  rodriiv01  2007      1  DET  AL  129  502   50  141   31    3  11
89396  ramirma02  2007      1  BOS  AL  133  483   84  143   33    1  20
89398  piazzmi01  2007      1  OAK  AL   83  309   33   85   17    1   8
89400  perezne01  2007      1  DET  AL   33   64    5   11    3    0   1
89402   parkch01  2007      1  NYN  NL    1    1    0    0    0    0   0
89406  oliveda02  2007      1  LAA  AL    5    0    0    0    0    0   0
89410  myersmi01  2007      1  NYA  AL    6    1    0    0    0    0   0
89411  mussimi01  2007      1  NYA  AL    2    2    0    0    0    0   0
89412  moyerja01  2007      1  PHI  NL   33   73    4    9    2    0   0
89420   mesajo01  2007      1  PHI  NL   38    0    0    0    0    0   0
89421  martipe02  2007      1  NYN  NL    5    9    1    1    1    0   0
89425  maddugr01  2007      1  SDN  NL   33   62    2    9    2    0   0
89426  mabryjo01  2007      1  COL  NL   28   34    4    4    1    0   1
89429  loftoke01  2007      2  CLE  AL   52  173   24   49    9    3   0
89430  loftoke01  2007      1  TEX  AL   84  317   62   96   16    3   7
89431  loaizes01  2007      1  LAN  NL    5    7    0    1    0    0   0
89438  kleskry01  2007      1  SFN  NL  116  362   51   94   27    3   6
89439   kentje01  2007      1  LAN  NL  136  494   78  149   36    1  20
89442  jonesto02  2007      1  DET  AL    5    0    0    0    0    0   0
89445  johnsra05  2007      1  ARI  NL   10   15    0    1    0    0   0
89450  hoffmtr01  2007      1  SDN  NL   60    0    0    0    0    0   0
89451  hernaro01  2007      2  LAN  NL   22    0    0    0    0    0   0
89452  hernaro01  2007      1  CLE  AL    2    0    0    0    0    0   0
89460  guarded01  2007      1  CIN  NL   15    0    0    0    0    0   0
89462  griffke02  2007      1  CIN  NL  144  528   78  146   24    1  30
89463  greensh01  2007      1  NYN  NL  130  446   62  130   30    1  10
89464  graffto01  2007      1  MIL  NL   86  231   34   55    8    0   9
89465  gordoto01  2007      1  PHI  NL   44    0    0    0    0    0   0
89466  gonzalu01  2007      1  LAN  NL  139  464   70  129   23    2  15
89467  gomezch02  2007      2  CLE  AL   19   53    4   15    2    0   0
89468  gomezch02  2007      1  BAL  AL   73  169   17   51   10    1   1
89469  glavito02  2007      1  NYN  NL   33   56    3   12    1    0   0
89473  floydcl01  2007      1  CHN  NL  108  282   40   80   10    1   9
89474  finlest01  2007      1  COL  NL   43   94    9   17    3    0   1
89480  embreal01  2007      1  OAK  AL    4    0    0    0    0    0   0
89481  edmonji01  2007      1  SLN  NL  117  365   39   92   15    2  12
89482  easleda01  2007      1  NYN  NL   76  193   24   54    6    0  10
89489  delgaca01  2007      1  NYN  NL  139  538   71  139   30    0  24
89493  cormirh01  2007      1  CIN  NL    6    0    0    0    0    0   0
89494  coninje01  2007      2  NYN  NL   21   41    2    8    2    0   0
89495  coninje01  2007      1  CIN  NL   80  215   23   57   11    1   6
89497  clemero02  2007      1  NYA  AL    2    2    0    1    0    0   0
89498  claytro01  2007      2  BOS  AL    8    6    1    0    0    0   0
89499  claytro01  2007      1  TOR  AL   69  189   23   48   14    0   1
89501  cirilje01  2007      2  ARI  NL   28   40    6    8    4    0   0
89502  cirilje01  2007      1  MIN  AL   50  153   18   40    9    2   2
89521  bondsba01  2007      1  SFN  NL  126  340   75   94   14    0  28
89523  biggicr01  2007      1  HOU  NL  141  517   68  130   31    3  10
89525  benitar01  2007      2  FLO  NL   34    0    0    0    0    0   0
89526  benitar01  2007      1  SFN  NL   19    0    0    0    0    0   0
89530  ausmubr01  2007      1  HOU  NL  117  349   38   82   16    3   3
89533   aloumo01  2007      1  NYN  NL   87  328   51  112   19    1  13
89534  alomasa02  2007      1  NYN  NL    8   22    1    3    1    0   0

New since 0.10.0, wide DataFrames will now be printed across multiple rows by default:

In [401]: DataFrame(randn(3, 12))
Out[401]: 
         0         1         2         3         4         5         6         7   \
0 -1.420521  1.616679 -1.030912  0.628297 -0.103189 -0.365475 -1.783911 -0.052526   
1  0.652300 -0.840516 -1.405878  0.966204  0.351162  0.048154  0.485560 -1.291041   
2  1.075002  0.526206  1.001947 -0.742489  0.767153  1.275288 -1.187808 -0.545179   
         8         9         10        11  
0 -1.408368  1.242233  0.952053 -0.172624  
1  0.122389  1.652711 -0.381662 -1.328093  
2  0.918173 -1.199261  0.371268  0.225621  

You can change how much to print on a single row by setting the line_width option:

In [402]: set_option('line_width', 40) # default is 80

In [403]: DataFrame(randn(3, 12))
Out[403]: 
         0         1         2         3   \
0 -0.970022 -1.127997 -0.384526 -0.492429   
1  1.295440  0.027006  0.863536  0.189023   
2 -0.822538 -1.590312 -0.061405  0.400325   
         4         5         6         7   \
0 -1.779882 -0.391166  0.575903 -1.343193   
1 -0.912154  0.946960 -0.257288  0.695208   
2  1.511027  0.289143  0.349037  1.998562   
         8         9         10        11  
0  1.646841  0.462269  1.078574  0.883532  
1  0.915200 -1.052414 -0.910945 -0.174453  
2  1.056844 -0.077851 -0.057005  0.626302  

You can also disable this feature via the expand_frame_repr option:

In [404]: set_option('expand_frame_repr', False)

In [405]: DataFrame(randn(3, 12))
Out[405]: 
<class 'pandas.core.frame.DataFrame'>
Int64Index: 3 entries, 0 to 2
Data columns:
0     3  non-null values
1     3  non-null values
2     3  non-null values
3     3  non-null values
4     3  non-null values
5     3  non-null values
6     3  non-null values
7     3  non-null values
8     3  non-null values
9     3  non-null values
10    3  non-null values
11    3  non-null values
dtypes: float64(12)

DataFrame column types

The four main types stored in pandas objects are float, int, boolean, and object. A convenient dtypes attribute return a Series with the data type of each column:

In [406]: baseball.dtypes
Out[406]: 
id        object
year       int64
stint      int64
team      object
lg        object
g          int64
ab         int64
r          int64
h          int64
X2b        int64
X3b        int64
hr         int64
rbi      float64
sb       float64
cs       float64
bb         int64
so       float64
ibb      float64
hbp      float64
sh       float64
sf       float64
gidp     float64
dtype: object

The related method get_dtype_counts will return the number of columns of each type:

In [407]: baseball.get_dtype_counts()
Out[407]: 
float64     9
int64      10
object      3
dtype: int64

DataFrame column attribute access and IPython completion

If a DataFrame column label is a valid Python variable name, the column can be accessed like attributes:

In [408]: df = DataFrame({'foo1' : np.random.randn(5),
   .....:                 'foo2' : np.random.randn(5)})
   .....:

In [409]: df
Out[409]: 
       foo1      foo2
0 -0.868315 -0.502919
1 -2.677551 -0.825049
2 -1.403487  0.518248
3 -0.561381 -0.438716
4  1.002897 -0.452045

In [410]: df.foo1
Out[410]: 
0   -0.868315
1   -2.677551
2   -1.403487
3   -0.561381
4    1.002897
Name: foo1, dtype: float64

The columns are also connected to the IPython completion mechanism so they can be tab-completed:

In [5]: df.fo<TAB>
df.foo1  df.foo2

Panel

Panel is a somewhat less-used, but still important container for 3-dimensional data. The term panel data is derived from econometrics and is partially responsible for the name pandas: pan(el)-da(ta)-s. The names for the 3 axes are intended to give some semantic meaning to describing operations involving panel data and, in particular, econometric analysis of panel data. However, for the strict purposes of slicing and dicing a collection of DataFrame objects, you may find the axis names slightly arbitrary:

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

Construction of Panels works about like you would expect:

From 3D ndarray with optional axis labels

In [411]: wp = Panel(randn(2, 5, 4), items=['Item1', 'Item2'],
   .....:            major_axis=date_range('1/1/2000', periods=5),
   .....:            minor_axis=['A', 'B', 'C', 'D'])
   .....:

In [412]: wp
Out[412]: 
<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

From dict of DataFrame objects

In [413]: data = {'Item1' : DataFrame(randn(4, 3)),
   .....:         'Item2' : DataFrame(randn(4, 2))}
   .....:

In [414]: Panel(data)
Out[414]: 
<class 'pandas.core.panel.Panel'>
Dimensions: 2 (items) x 4 (major_axis) x 3 (minor_axis)
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 DataFrame. Thus, they can be any of the other valid inputs to DataFrame as per above.

One helpful factory method is Panel.from_dict, which takes a dictionary of DataFrames as above, and the following named parameters:

Parameter Default Description
intersect False drops elements whose indices do not align
orient items use minor to use DataFrames’ columns as panel items

For example, compare to the construction above:

In [415]: Panel.from_dict(data, orient='minor')
Out[415]: 
<class 'pandas.core.panel.Panel'>
Dimensions: 3 (items) x 4 (major_axis) x 2 (minor_axis)
Items axis: 0 to 2
Major_axis axis: 0 to 3
Minor_axis axis: Item1 to Item2

Orient is especially useful for mixed-type DataFrames. If you pass a dict of DataFrame objects with mixed-type columns, all of the data will get upcasted to dtype=object unless you pass orient='minor':

In [416]: df = DataFrame({'a': ['foo', 'bar', 'baz'],
   .....:                 'b': np.random.randn(3)})
   .....:

In [417]: df
Out[417]: 
     a         b
0  foo  1.448717
1  bar  0.608653
2  baz -1.409338

In [418]: data = {'item1': df, 'item2': df}

In [419]: panel = Panel.from_dict(data, orient='minor')

In [420]: panel['a']
Out[420]: 
  item1 item2
0   foo   foo
1   bar   bar
2   baz   baz

In [421]: panel['b']
Out[421]: 
      item1     item2
0  1.448717  1.448717
1  0.608653  0.608653
2 -1.409338 -1.409338

In [422]: panel['b'].dtypes
Out[422]: 
item1    float64
item2    float64
dtype: object

Note

Unfortunately Panel, being less commonly used than Series and DataFrame, has been slightly neglected feature-wise. A number of methods and options available in DataFrame are not available in Panel. This will get worked on, of course, in future releases. And faster if you join me in working on the codebase.

From DataFrame using to_panel method

This method was introduced in v0.7 to replace LongPanel.to_long, and converts a DataFrame with a two-level index to a Panel.

In [423]: midx = MultiIndex(levels=[['one', 'two'], ['x','y']], labels=[[1,1,0,0],[1,0,1,0]])

In [424]: df = DataFrame({'A' : [1, 2, 3, 4], 'B': [5, 6, 7, 8]}, index=midx)

In [425]: df.to_panel()
Out[425]: 
<class 'pandas.core.panel.Panel'>
Dimensions: 2 (items) x 2 (major_axis) x 2 (minor_axis)
Items axis: A to B
Major_axis axis: one to two
Minor_axis axis: x to y

Item selection / addition / deletion

Similar to DataFrame functioning as a dict of Series, Panel is like a dict of DataFrames:

In [426]: wp['Item1']
Out[426]: 
                   A         B         C         D
2000-01-01 -1.362139 -0.098512 -0.491067  0.048491
2000-01-02  2.287810 -0.403876 -1.076283 -0.155956
2000-01-03  0.388741 -1.284588 -0.508030  0.841173
2000-01-04 -0.555843 -0.030913 -0.289758  1.318467
2000-01-05  1.025903  0.195796  0.030198 -0.349406

In [427]: wp['Item3'] = wp['Item1'] / wp['Item2']

The API for insertion and deletion is the same as for DataFrame. And as with DataFrame, if the item is a valid python identifier, you can access it as an attribute and tab-complete it in IPython.

Transposing

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

In [428]: wp.transpose(2, 0, 1)
Out[428]: 
<class 'pandas.core.panel.Panel'>
Dimensions: 4 (items) x 3 (major_axis) x 5 (minor_axis)
Items axis: A to D
Major_axis axis: Item1 to Item3
Minor_axis axis: 2000-01-01 00:00:00 to 2000-01-05 00:00:00

Indexing / Selection

Operation Syntax Result
Select item wp[item] DataFrame
Get slice at major_axis label wp.major_xs(val) DataFrame
Get slice at minor_axis label wp.minor_xs(val) DataFrame

For example, using the earlier example data, we could do:

In [429]: wp['Item1']
Out[429]: 
                   A         B         C         D
2000-01-01 -1.362139 -0.098512 -0.491067  0.048491
2000-01-02  2.287810 -0.403876 -1.076283 -0.155956
2000-01-03  0.388741 -1.284588 -0.508030  0.841173
2000-01-04 -0.555843 -0.030913 -0.289758  1.318467
2000-01-05  1.025903  0.195796  0.030198 -0.349406

In [430]: wp.major_xs(wp.major_axis[2])
Out[430]: 
      Item1     Item2     Item3
A  0.388741  1.076202  0.361216
B -1.284588 -0.464905  2.763121
C -0.508030  0.432658 -1.174206
D  0.841173 -0.623043 -1.350105

In [431]: wp.minor_axis
Out[431]: Index([A, B, C, D], dtype=object)

In [432]: wp.minor_xs('C')
Out[432]: 
               Item1     Item2     Item3
2000-01-01 -0.491067 -0.530157  0.926267
2000-01-02 -1.076283 -0.692498  1.554205
2000-01-03 -0.508030  0.432658 -1.174206
2000-01-04 -0.289758  1.771692 -0.163549
2000-01-05  0.030198 -0.016490 -1.831272

Conversion to DataFrame

A Panel can be represented in 2D form as a hierarchically indexed DataFrame. See the section hierarchical indexing for more on this. To convert a Panel to a DataFrame, use the to_frame method:

In [433]: panel = Panel(np.random.randn(3, 5, 4), items=['one', 'two', 'three'],
   .....:               major_axis=date_range('1/1/2000', periods=5),
   .....:               minor_axis=['a', 'b', 'c', 'd'])
   .....:

In [434]: panel.to_frame()
Out[434]: 
                       one       two     three
major      minor                              
2000-01-01 a      1.219834 -0.842503  1.130688
           b     -0.185793 -0.585949  1.348831
           c     -1.016665 -0.864916 -0.709279
           d      0.170971  0.031573 -0.125291
2000-01-02 a      0.411316 -1.170645 -1.746865
           b     -0.773663 -0.655575 -0.802833
           c     -0.028610 -1.297237 -0.824150
           d      0.532592 -1.739996 -0.056603
2000-01-03 a      0.579638 -0.093661  1.443225
           b     -1.514892  0.873783 -1.013384
           c      1.528058 -1.803206 -0.591932
           d      0.954347 -0.134374  0.679775
2000-01-04 a     -0.635819 -0.100241 -0.921819
           b      0.009356  0.837864 -0.549326
           c     -1.639594  1.326922  0.273431
           d     -0.699057  1.224153 -0.189170
2000-01-05 a     -1.027240 -0.856153 -1.699029
           b     -0.489779 -0.038563  0.589825
           c      0.046080  0.521149 -0.065580
           d     -0.104689 -1.270389  0.877266

Panel4D (Experimental)

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

From 4D ndarray with optional axis labels

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

In [436]: p4d
Out[436]: 
<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

From dict of Panel objects

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

In [438]: Panel4D(data)
Out[438]: 
<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.

Slicing

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

In [439]: p4d['Label1']
Out[439]: 
<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 [440]: p4d.ix[:,:,:,'A']
Out[440]: 
<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 [441]: p4d.ix[:,:,0,'A']
Out[441]: 
         Label1    Label2
Item1  2.301716 -0.045494
Item2  1.454477  1.420470

4D -> Series

In [442]: p4d.ix[:,0,0,'A']
Out[442]: 
Label1    2.301716
Label2   -0.045494
Name: A, dtype: float64

Transposing

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

In [443]: p4d.transpose(3, 2, 1, 0)
Out[443]: 
<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

PanelND (Experimental)

PanelND is a module with a set of factory functions to enable a user to construct N-dimensional named containers like Panel4D, with a custom set of axis labels. Thus a domain-specific container can easily be created.

The following creates a Panel5D. A new panel type object must be sliceable into a lower dimensional object. Here we slice to a Panel4D.

In [444]: from pandas.core import panelnd

In [445]: Panel5D = panelnd.create_nd_panel_factory(
   .....:     klass_name   = 'Panel5D',
   .....:     axis_orders  = [ 'cool', 'labels','items','major_axis','minor_axis'],
   .....:     axis_slices  = { 'labels' : 'labels', 'items' : 'items',
   .....:                      'major_axis' : 'major_axis', 'minor_axis' : 'minor_axis' },
   .....:     slicer       = Panel4D,
   .....:     axis_aliases = { 'major' : 'major_axis', 'minor' : 'minor_axis' },
   .....:     stat_axis    = 2)
   .....:

In [446]: p5d = Panel5D(dict(C1 = p4d))

In [447]: p5d
Out[447]: 
<class 'pandas.core.panelnd.Panel5D'>
Dimensions: 1 (cool) x 2 (labels) x 2 (items) x 5 (major_axis) x 4 (minor_axis)
Cool axis: C1 to C1
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

# print a slice of our 5D
In [448]: p5d.ix['C1',:,:,0:3,:]
Out[448]: 
<class 'pandas.core.panelnd.Panel4D'>
Dimensions: 2 (labels) x 2 (items) x 3 (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-03 00:00:00
Minor_axis axis: A to D

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

# look at the shape & dim
In [450]: p5d.shape
Out[450]: [1, 2, 2, 5, 4]

In [451]: p5d.ndim
Out[451]: 5