Working with pandas

One of the most important features of xarray is the ability to convert to and from pandas objects to interact with the rest of the PyData ecosystem. For example, for plotting labeled data, we highly recommend using the visualization built in to pandas itself or provided by the pandas aware libraries such as Seaborn.

Hierarchical and tidy data

Tabular data is easiest to work with when it meets the criteria for tidy data:

  • Each column holds a different variable.
  • Each rows holds a different observation.

In this “tidy data” format, we can represent any Dataset and DataArray in terms of pandas.DataFrame and pandas.Series, respectively (and vice-versa). The representation works by flattening non-coordinates to 1D, and turning the tensor product of coordinate indexes into a pandas.MultiIndex.

Dataset and DataFrame

To convert any dataset to a DataFrame in tidy form, use the Dataset.to_dataframe() method:

In [1]: ds = xr.Dataset({'foo': (('x', 'y'), np.random.randn(2, 3))},
   ...:                  coords={'x': [10, 20], 'y': ['a', 'b', 'c'],
   ...:                          'along_x': ('x', np.random.randn(2)),
   ...:                          'scalar': 123})

In [2]: ds
Dimensions:  (x: 2, y: 3)
  * y        (y) |S1 'a' 'b' 'c'
  * x        (x) int64 10 20
    scalar   int64 123
    along_x  (x) float64 0.1192 -1.044
Data variables:
    foo      (x, y) float64 0.4691 -0.2829 -1.509 -1.136 1.212 -0.1732

In [3]: df = ds.to_dataframe()

In [4]: df
           foo  scalar   along_x
x  y                            
10 a  0.469112     123  0.119209
   b -0.282863     123  0.119209
   c -1.509059     123  0.119209
20 a -1.135632     123 -1.044236
   b  1.212112     123 -1.044236
   c -0.173215     123 -1.044236

We see that each variable and coordinate in the Dataset is now a column in the DataFrame, with the exception of indexes which are in the index. To convert the DataFrame to any other convenient representation, use DataFrame methods like reset_index(), stack() and unstack().

To create a Dataset from a DataFrame, use the from_dataframe() class method or the equivalent pandas.DataFrame.to_xarray method (pandas v0.18 or later):

In [5]: xr.Dataset.from_dataframe(df)
Dimensions:  (x: 2, y: 3)
  * x        (x) int64 10 20
  * y        (y) object 'a' 'b' 'c'
Data variables:
    foo      (x, y) float64 0.4691 -0.2829 -1.509 -1.136 1.212 -0.1732
    scalar   (x, y) int64 123 123 123 123 123 123
    along_x  (x, y) float64 0.1192 0.1192 0.1192 -1.044 -1.044 -1.044

Notice that that dimensions of variables in the Dataset have now expanded after the round-trip conversion to a DataFrame. This is because every object in a DataFrame must have the same indices, so we need to broadcast the data of each array to the full size of the new MultiIndex.

Likewise, all the coordinates (other than indexes) ended up as variables, because pandas does not distinguish non-index coordinates.

DataArray and Series

DataArray objects have a complementary representation in terms of a pandas.Series. Using a Series preserves the Dataset to DataArray relationship, because DataFrames are dict-like containers of Series. The methods are very similar to those for working with DataFrames:

In [6]: s = ds['foo'].to_series()

In [7]: s
x   y
10  a    0.469112
    b   -0.282863
    c   -1.509059
20  a   -1.135632
    b    1.212112
    c   -0.173215
Name: foo, dtype: float64

# or equivalently, with Series.to_xarray()
In [8]: xr.DataArray.from_series(s)
<xarray.DataArray 'foo' (x: 2, y: 3)>
array([[ 0.469, -0.283, -1.509],
       [-1.136,  1.212, -0.173]])
  * x        (x) int64 10 20
  * y        (y) object 'a' 'b' 'c'

Both the from_series and from_dataframe methods use reindexing, so they work even if not the hierarchical index is not a full tensor product:

In [9]: s[::2]
x   y
10  a    0.469112
    c   -1.509059
20  b    1.212112
Name: foo, dtype: float64

In [10]: s[::2].to_xarray()
<xarray.DataArray 'foo' (x: 2, y: 3)>
array([[ 0.469,    nan, -1.509],
       [   nan,  1.212,    nan]])
  * x        (x) int64 10 20
  * y        (y) object 'a' 'b' 'c'

Multi-dimensional data

Tidy data is great, but it sometimes you want to preserve dimensions instead of automatically stacking them into a MultiIndex.

DataArray.to_pandas() is a shortcut that lets you convert a DataArray directly into a pandas object with the same dimensionality (i.e., a 1D array is converted to a Series, 2D to DataFrame and 3D to Panel):

In [11]: arr = xr.DataArray(np.random.randn(2, 3),
   ....:                    coords=[('x', [10, 20]), ('y', ['a', 'b', 'c'])])

In [12]: df = arr.to_pandas()

In [13]: df
y          a         b         c
10 -0.861849 -2.104569 -0.494929
20  1.071804  0.721555 -0.706771

To perform the inverse operation of converting any pandas objects into a data array with the same shape, simply use the DataArray constructor:

In [14]: xr.DataArray(df)
<xarray.DataArray (x: 2, y: 3)>
array([[-0.862, -2.105, -0.495],
       [ 1.072,  0.722, -0.707]])
  * x        (x) int64 10 20
  * y        (y) object 'a' 'b' 'c'

Both the DataArray and Dataset constructors directly convert pandas objects into xarray objects with the same shape. This means that they preserve all use of multi-indexes:

In [15]: index = pd.MultiIndex.from_arrays([['a', 'a', 'b'], [0, 1, 2]],
   ....:                                   names=['one', 'two'])

In [16]: df = pd.DataFrame({'x': 1, 'y': 2}, index=index)

In [17]: ds = xr.Dataset(df)

In [18]: ds
Dimensions:  (dim_0: 3)
  * dim_0    (dim_0) object ('a', 0) ('a', 1) ('b', 2)
Data variables:
    x        (dim_0) int64 1 1 1
    y        (dim_0) int64 2 2 2

However, you will need to set dimension names explicitly, either with the dims argument on in the DataArray constructor or by calling rename on the new object.

Transitioning from pandas.Panel to xarray

Panel, pandas’s data structure for 3D arrays, has always been a second class data structure compared to the Series and DataFrame. To allow pandas developers to focus more on its core functionality built around the DataFrame, pandas plans to eventually deprecate Panel.

xarray has most of Panel‘s features, a more explicit API (particularly around indexing), and the ability to scale to >3 dimensions with the same interface.

As discussed elsewhere in the docs, there are two primary data structures in xarray: DataArray and Dataset. You can imagine a DataArray as a n-dimensional pandas Series (i.e. a single typed array), and a Dataset as the DataFrame equivalent (i.e. a dict of aligned DataArray objects).

So you can represent a Panel, in two ways:

  • As a 3-dimensional DataArray,
  • Or as a Dataset containing a number of 2-dimensional DataArray objects.

Let’s take a look:

In [19]: panel = pd.Panel(np.random.rand(2, 3, 4), items=list('ab'), major_axis=list('mno'),
   ....:                  minor_axis=pd.date_range(start='2000', periods=4, name='date'))

In [20]: panel
<class 'pandas.core.panel.Panel'>
Dimensions: 2 (items) x 3 (major_axis) x 4 (minor_axis)
Items axis: a to b
Major_axis axis: m to o
Minor_axis axis: 2000-01-01 00:00:00 to 2000-01-04 00:00:00

As a DataArray:

# or equivalently, with Panel.to_xarray()
In [21]: xr.DataArray(panel)
<xarray.DataArray (dim_0: 2, dim_1: 3, date: 4)>
array([[[ 0.595,  0.138,  0.853,  0.236],
        [ 0.146,  0.59 ,  0.574,  0.061],
        [ 0.59 ,  0.245,  0.34 ,  0.985]],

       [[ 0.92 ,  0.038,  0.862,  0.754],
        [ 0.405,  0.344,  0.171,  0.395],
        [ 0.642,  0.275,  0.462,  0.871]]])
  * dim_0    (dim_0) object 'a' 'b'
  * dim_1    (dim_1) object 'm' 'n' 'o'
  * date     (date) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03 2000-01-04

As you can see, there are three dimensions (each is also a coordinate). Two of the axes of the panel were unnamed, so have been assigned dim_0 and dim_1 respectively, while the third retains its name date.

As a Dataset:

In [22]: xr.Dataset(panel)
Dimensions:  (date: 4, dim_0: 3)
  * dim_0    (dim_0) object 'm' 'n' 'o'
  * date     (date) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03 2000-01-04
Data variables:
    a        (dim_0, date) float64 0.5948 0.1376 0.8529 0.2355 0.1462 0.5899 ...
    b        (dim_0, date) float64 0.9195 0.03777 0.8615 0.7536 0.4052 ...

Here, there are two data variables, each representing a DataFrame on panel’s items axis, and labelled as such. Each variable is a 2D array of the respective values along the items dimension.

While the xarray docs are relatively complete, a few items stand out for Panel users:

  • A DataArray’s data is stored as a numpy array, and so can only contain a single type. As a result, a Panel that contains DataFrame objects with multiple types will be converted to dtype=object. A Dataset of multiple DataArray objects each with its own dtype will allow original types to be preserved.
  • Indexing is similar to pandas, but more explicit and leverages xarray’s naming of dimensions.
  • Because of those features, making much higher dimensional data is very practical.
  • Variables in Dataset objects can use a subset of its dimensions. For example, you can have one dataset with Person x Score x Time, and another with Person x Score.
  • You can use coordinates are used for both dimensions and for variables which _label_ the data variables, so you could have a coordinate Age, that labelled the Person dimension of a Dataset of Person x Score x Time.

While xarray may take some getting used to, it’s worth it! If anything is unclear, please post an issue on GitHub or StackOverflow, and we’ll endeavor to respond to the specific case or improve the general docs.