Quick overview¶
Here are some quick examples of what you can do with xarray.DataArray
objects. Everything is explained in much more detail in the rest of the
documentation.
To begin, import numpy, pandas and xarray using their customary abbreviations:
In [1]: import numpy as np
In [2]: import pandas as pd
In [3]: import xarray as xr
Create a DataArray¶
You can make a DataArray from scratch by supplying data in the form of a numpy array or list, with optional dimensions and coordinates:
In [4]: xr.DataArray(np.random.randn(2, 3))
Out[4]:
<xarray.DataArray (dim_0: 2, dim_1: 3)>
array([[ 1.643563, -1.469388, 0.357021],
[-0.6746 , -1.776904, -0.968914]])
Dimensions without coordinates: dim_0, dim_1
In [5]: data = xr.DataArray(np.random.randn(2, 3), coords={'x': ['a', 'b']}, dims=('x', 'y'))
In [6]: data
Out[6]:
<xarray.DataArray (x: 2, y: 3)>
array([[-1.294524, 0.413738, 0.276662],
[-0.472035, -0.01396 , -0.362543]])
Coordinates:
* x (x) <U1 'a' 'b'
Dimensions without coordinates: y
If you supply a pandas Series
or
DataFrame
, metadata is copied directly:
In [7]: xr.DataArray(pd.Series(range(3), index=list('abc'), name='foo'))
Out[7]:
<xarray.DataArray 'foo' (dim_0: 3)>
array([0, 1, 2])
Coordinates:
* dim_0 (dim_0) object 'a' 'b' 'c'
Here are the key properties for a DataArray
:
# like in pandas, values is a numpy array that you can modify in-place
In [8]: data.values
Out[8]:
array([[-1.295, 0.414, 0.277],
[-0.472, -0.014, -0.363]])
In [9]: data.dims