In [3]:
%matplotlib inline
import numpy as np
import xarray as xr
In [4]:
arr = xr.DataArray(np.random.randn(2, 3), [('x', ['a', 'b']), ('y', [10, 20, 30])])
In [5]:
arr - 3
Out[5]:
In [6]:
abs(arr)
Out[6]:
You can also use any of numpy’s or scipy’s many ufunc functions directly on a DataArray:
In [8]:
np.sin(arr)
Out[8]:
Data arrays also implement many numpy.ndarray
methods:
In [9]:
arr.round(2)
Out[9]:
In [10]:
arr.T
Out[10]:
In [11]:
x = xr.DataArray([0, 1, np.nan, np.nan, 2], dims=['x'])
In [12]:
x.isnull()
Out[12]:
In [13]:
x.notnull()
Out[13]:
In [14]:
x.count()
Out[14]:
In [15]:
x.dropna(dim='x')
Out[15]:
In [16]:
x.fillna(-1)
Out[16]:
Like pandas, xarray uses the float value np.nan
(not-a-number) to represent missing values.
In [17]:
arr.sum(dim='x')
Out[17]:
In [18]:
arr.std(['x', 'y'])
Out[18]:
In [19]:
arr.min()
Out[19]:
If you need to figure out the axis number for a dimension yourself (say, for wrapping code designed to work with numpy arrays), you can use the get_axis_num()
method:
In [21]:
arr.get_axis_num('y')
Out[21]:
These operations automatically skip missing values, like in pandas:
In [22]:
xr.DataArray([1, 2, np.nan, 3]).mean()
Out[22]:
If desired, you can disable this behavior by invoking the aggregation method with skipna=False
.
In [23]:
arr = xr.DataArray(np.arange(0, 7.5, 0.5).reshape(3, 5), dims=('x', 'y'))
In [24]:
arr
Out[24]:
rolling()
is applied along one dimension using the name of the dimension as a key (e.g. y
) and the window size as the value (e.g. 3
). We get back a Rolling
object:
In [25]:
arr.rolling(y=3)
Out[25]:
The label position and minimum number of periods in the rolling window are controlled by the center
and min_periods
arguments:
In [48]:
arr.rolling(y=3, min_periods=2, center=True)
Out[48]:
In [49]:
r = arr.rolling(y=3)
In [50]:
r.mean()
Out[50]:
In [47]:
r.reduce(np.std)
Out[47]:
Note that rolling window aggregations are much faster (both asymptotically and because they avoid a loop in Python) when bottleneck is installed. Otherwise, we fall back to a slower, pure Python implementation.
Finally, we can manually iterate through Rolling
objects:
In [36]:
for label, arr_window in r:
print('==============================\n', label, '\n-------------------------\n', arr_window)
DataArray
objects are automatically align themselves (“broadcasting” in the numpy parlance) by dimension name instead of axis order. With xarray, you do not need to transpose arrays or insert dimensions of length 1 to get array operations to work, as commonly done in numpy with np.reshape()
or np.newaxis
.
This is best illustrated by a few examples. Consider two one-dimensional arrays with different sizes aligned along different dimensions:
In [51]:
a = xr.DataArray([1, 2], [('x', ['a', 'b'])])
In [52]:
a
Out[52]:
In [53]:
b = xr.DataArray([-1, -2, -3], [('y', [10, 20, 30])])
In [54]:
b
Out[54]:
With xarray, we can apply binary mathematical operations to these arrays, and their dimensions are expanded automatically:
In [55]:
a * b
Out[55]:
Moreover, dimensions are always reordered to the order in which they first appeared:
In [56]:
c = xr.DataArray(np.arange(6).reshape(3, 2), [b['y'], a['x']])
In [57]:
c
Out[57]:
In [58]:
a + c
Out[58]:
This means, for example, that you always subtract an array from its transpose:
In [59]:
c - c.T
Out[59]:
In [ ]: