Our goal is to provide a pandas-like and pandas-compatible toolkit for analytics on multi-dimensional arrays, rather than the tabular data for which pandas excels. Our approach adopts the Common Data Model for self- describing scientific data in widespread use in the Earth sciences:
xarray.Dataset is an in-memory representation of a netCDF file.Adding dimensions names and coordinate indexes to numpy’s ndarray makes many powerful array operations possible:
- Apply operations over dimensions by name:
x.sum('time'). - Select values by label instead of integer location:
x.loc['2014-01-01']orx.sel(time='2014-01-01'). - Mathematical operations (e.g.,
x - y) vectorize across multiple dimensions (array broadcasting) based on dimension names, not shape. - Flexible split-apply-combine operations with groupby:
x.groupby('time.dayofyear').mean(). - Database like alignment based on coordinate labels that smoothly
handles missing values:
x, y = xr.align(x, y, join='outer'). - Keep track of arbitrary metadata in the form of a Python dictionary:
x.attrs.
The N-dimensional nature of xarray’s data structures makes it suitable for dealing with multi-dimensional scientific data, and its use of dimension names instead of axis labels (
dim='time' instead of axis=0) makes such
arrays much more manageable than the raw numpy ndarray: with xarray, you don’t
need to keep track of the order of arrays dimensions or insert dummy dimensions
(e.g., np.newaxis) to align arrays.xarray has two core data structures. Both are fundamentally N-dimensional:
DataArrayis our implementation of a labeled, N-dimensional array. It is an N-D generalization of apandas.Series. The nameDataArrayitself is borrowed from Fernando Perez’s datarray project, which prototyped a similar data structure.Datasetis a multi-dimensional, in-memory array database. It is a dict-like container ofDataArrayobjects aligned along any number of shared dimensions, and serves a similar purpose in xarray to thepandas.DataFrame.
numpy.ndarray may be
fairly obvious, but the dataset may need more motivation.The power of the dataset over a plain dictionary is that, in addition to pulling out arrays by name, it is possible to select or combine data along a dimension across all arrays simultaneously. Like a
DataFrame, datasets facilitate array operations with
heterogeneous data – the difference is that the arrays in a dataset can not
only have different data types, but can also have different numbers of
dimensions.This data model is borrowed from the netCDF file format, which also provides xarray with a natural and portable serialization format. NetCDF is very popular in the geosciences, and there are existing libraries for reading and writing netCDF in many programming languages, including Python.
xarray distinguishes itself from many tools for working with netCDF data in-so-far as it provides data structures for in-memory analytics that both utilize and preserve labels. You only need to do the tedious work of adding metadata once, not every time you save a file."
http://xarray.pydata.org/en/stable/
https://github.com/Unidata/unidata-users-workshop/blob/master/notebooks/xray-tutorial.ipynb
http://nbviewer.jupyter.org/github/nicolasfauchereau/metocean/blob/master/notebooks/xray.ipynb
xarray: N-D labeled Arrays and Datasets in Python - http://openresearchsoftware.metajnl.com/articles/10.5334/jors.148/
No comments:
Post a Comment