xarray.Coordinates#
- class xarray.Coordinates(coords=None, indexes=None)[source]#
Dictionary like container for Xarray coordinates (variables + indexes).
This collection is a mapping of coordinate names to
DataArray
objects.It can be passed directly to the
Dataset
andDataArray
constructors via their coords argument. This will add both the coordinates variables and their index.Coordinates are either:
returned via the
Dataset.coords
,DataArray.coords
, andDataTree.coords
properties,built from Xarray or Pandas index objects (e.g.,
Coordinates.from_xindex()
orCoordinates.from_pandas_multiindex()
),built manually from input coordinate data and Xarray
Index
objects viaCoordinates.__init__()
(beware that no consistency check is done on those inputs).
To create new coordinates from an existing Xarray
Index
object, useCoordinates.from_xindex()
instead ofCoordinates.__init__()
. The latter is useful, e.g., for creating coordinates with no default index.- Parameters:
coords (dict-like, optional) – Mapping where keys are coordinate names and values are objects that can be converted into a
Variable
object (seeas_variable()
). If anotherCoordinates
object is passed, its indexes will be added to the new created object.indexes (dict-like, optional) – Mapping where keys are coordinate names and values are
Index
objects. If None (default), pandas indexes will be created for each dimension coordinate. Passing an empty dictionary will skip this default behavior.
Examples
Create a dimension coordinate with a default (pandas) index:
>>> xr.Coordinates({"x": [1, 2]}) Coordinates: * x (x) int64 16B 1 2
Create a dimension coordinate with no index:
>>> xr.Coordinates(coords={"x": [1, 2]}, indexes={}) Coordinates: x (x) int64 16B 1 2
Create a new Coordinates object from existing dataset coordinates (indexes are passed):
>>> ds = xr.Dataset(coords={"x": [1, 2]}) >>> xr.Coordinates(ds.coords) Coordinates: * x (x) int64 16B 1 2
Create indexed coordinates from a
pandas.MultiIndex
object:>>> midx = pd.MultiIndex.from_product([["a", "b"], [0, 1]]) >>> xr.Coordinates.from_pandas_multiindex(midx, "x") Coordinates: * x (x) object 32B MultiIndex * x_level_0 (x) object 32B 'a' 'a' 'b' 'b' * x_level_1 (x) int64 32B 0 1 0 1
Create a new Dataset object by passing a Coordinates object:
>>> midx_coords = xr.Coordinates.from_pandas_multiindex(midx, "x") >>> xr.Dataset(coords=midx_coords) <xarray.Dataset> Size: 96B Dimensions: (x: 4) Coordinates: * x (x) object 32B MultiIndex * x_level_0 (x) object 32B 'a' 'a' 'b' 'b' * x_level_1 (x) int64 32B 0 1 0 1 Data variables: *empty*
Methods
__init__
([coords, indexes])assign
([coords])Assign new coordinates (and indexes) to a Coordinates object, returning a new object with all the original coordinates in addition to the new ones.
copy
([deep, memo])Return a copy of this Coordinates object.
equals
(other)Two Coordinates objects are equal if they have matching variables, all of which are equal.
from_pandas_multiindex
(midx, dim)Wrap a pandas multi-index as Xarray coordinates (dimension + levels).
from_xindex
(index)Create Xarray coordinates from an existing Xarray index.
get
(k[,d])identical
(other)Like equals, but also checks all variable attributes.
items
()keys
()merge
(other)Merge two sets of coordinates to create a new Dataset
Convert these coordinates into a new Dataset.
to_index
([ordered_dims])Convert all index coordinates into a
pandas.Index
.update
(other)Update this Coordinates variables with other coordinate variables.
values
()Attributes
Mapping from dimension names to lengths or tuple of dimension names.
Mapping from coordinate names to dtypes.
Mapping of pandas.Index objects used for label based indexing.
Mapping from dimension names to lengths.
Low level interface to Coordinates contents as dict of Variable objects.
Mapping of
Index
objects used for label based indexing.