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]: data = xr.DataArray(np.random.randn(2, 3),
...: dims=('x', 'y'),
...: coords={'x': [10, 20]})
...:
In [5]: data
Out[5]:
<xarray.DataArray (x: 2, y: 3)>
array([[ 1.643563, -1.469388, 0.357021],
[-0.6746 , -1.776904, -0.968914]])
Coordinates:
* x (x) int64 10 20
Dimensions without coordinates: y
In this case, we have generated a 2D array, assigned the names x and y to the two dimensions respectively and associated two coordinate labels ‘10’ and ‘20’ with the two locations along the x dimension. If you supply a pandas Series
or DataFrame
, metadata is copied directly:
In [6]: xr.DataArray(pd.Series(range(3), index=list('abc'), name='foo'))
Out[6]:
<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 [7]: data.values
Out[7]:
array([[ 1.644, -1.469, 0.357],
[-0.675, -1.777, -0.969]])
In [8]: data.dims