<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 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
Step1:  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
Step2:  If you supply a pandas Series or DataFrame, metadata is copied directly
Step3:  Here are the key properties for a DataArray
Step4:  Indexing
xarray supports four kind of indexing. These operations are just as fast as in pandas, because we borrow pandas’ indexing machinery.
Step5:  Computation
Data arrays work very similarly to numpy ndarrays
Step6:  However, aggregation operations can use dimension names instead of axis numbers
Step7:  Arithmetic operations broadcast based on dimension name. This means you don’t need to insert dummy dimensions for alignment
Step8:  Another broadcast example
Step9:  It also means that in most cases you do not need to worry about the order of dimensions
Step10:  Operations also align based on index labels
Step11:  GroupBy
xarray supports grouped operations using a very similar API to pandas
Step12:  Convert to pandas
A key feature of xarray is robust conversion to and from pandas objects
Step13:  Datasets and NetCDF
xarray.Dataset is a dict-like container of DataArray objects that share index labels and dimensions. It looks a lot like a netCDF file
Step14:  You can do almost everything you can do with DataArray objects with Dataset objects if you prefer to work with multiple variables at once.
Datasets also let you easily read and write netCDF files
<ASSISTANT_TASK:>
Python Code:
import numpy as np
import pandas as pd
import xarray as xr
Explanation: 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:
End of explanation
xr.DataArray(np.random.randn(2, 3))
data = xr.DataArray(np.random.randn(2, 3), [('x', ['a', 'b']), ('y', [-2, 0, 2])])
data
Explanation: 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:
End of explanation
xr.DataArray(pd.Series(range(3), index=list('abc'), name='foo'))
Explanation: If you supply a pandas Series or DataFrame, metadata is copied directly:
End of explanation
data.values
data.dims
data.coords
len(data.coords)
data.coords['x']
data.attrs
Explanation: Here are the key properties for a DataArray:
End of explanation
data[[0, 1]]
data.loc['a':'b']
data.loc
data.isel(x=slice(2))
data.sel(x=['a', 'b'])
Explanation: Indexing
xarray supports four kind of indexing. These operations are just as fast as in pandas, because we borrow pandas’ indexing machinery.
End of explanation
data
data + 10
np.sin(data)
data.T
data.sum()
Explanation: Computation
Data arrays work very similarly to numpy ndarrays:
End of explanation
data.mean(dim='x')
Explanation: However, aggregation operations can use dimension names instead of axis numbers:
End of explanation
a = xr.DataArray(np.random.randn(3), [data.coords['y']])
b = xr.DataArray(np.random.randn(4), dims='z')
a
b
a + b
Explanation: Arithmetic operations broadcast based on dimension name. This means you don’t need to insert dummy dimensions for alignment:
End of explanation
v1 = xr.DataArray(np.random.rand(3, 2, 4), dims=['t', 'y', 'x'])
v2 = xr.DataArray(np.random.rand(2, 4), dims=['y', 'x'])
v1
v2
v1 + v2
Explanation: Another broadcast example:
End of explanation
data - data.T
Explanation: It also means that in most cases you do not need to worry about the order of dimensions:
End of explanation
data[:-1]
data[:1]
data[:-1] - data[:1]
Explanation: Operations also align based on index labels:
End of explanation
labels = xr.DataArray(['E', 'F', 'E'], [data.coords['y']], name='labels')
labels
data
data.groupby(labels).mean('y')
data.groupby(labels).apply(lambda x: x - x.min())
Explanation: GroupBy
xarray supports grouped operations using a very similar API to pandas:
End of explanation
data.to_series()
data.to_pandas()
Explanation: Convert to pandas
A key feature of xarray is robust conversion to and from pandas objects:
End of explanation
ds = data.to_dataset(name='foo')
ds
Explanation: Datasets and NetCDF
xarray.Dataset is a dict-like container of DataArray objects that share index labels and dimensions. It looks a lot like a netCDF file:
End of explanation
ds.to_netcdf('example.nc')
xr.open_dataset('example.nc')
Explanation: You can do almost everything you can do with DataArray objects with Dataset objects if you prefer to work with multiple variables at once.
Datasets also let you easily read and write netCDF files:
End of explanation
<END_TASK>