🍾 Xarray is now 10 years old! 🎉

xarray.Coordinates

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 and DataArray constructors via their coords argument. This will add both the coordinates variables and their index.

Coordinates are either:

Parameters:
  • coords (dict-like, optional) – Mapping where keys are coordinate names and values are objects that can be converted into a Variable object (see as_variable()). If another Coordinates 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*
__init__(coords=None, indexes=None)[source]#

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).

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

to_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

dims

Mapping from dimension names to lengths or tuple of dimension names.

dtypes

Mapping from coordinate names to dtypes.

indexes

Mapping of pandas.Index objects used for label based indexing.

sizes

Mapping from dimension names to lengths.

variables

Low level interface to Coordinates contents as dict of Variable objects.

xindexes

Mapping of Index objects used for label based indexing.