xarray.indexes.NDPointIndex#
- class xarray.indexes.NDPointIndex(tree_obj, *, coord_names, dims, shape)[source]#
Xarray index for irregular, n-dimensional data.
This index may be associated with a set of coordinate variables representing the arbitrary location of data points in an n-dimensional space. All coordinates must have the same shape and dimensions. The number of associated coordinate variables must correspond to the number of dimensions of the space.
This index supports label-based selection (nearest neighbor lookup). It also has limited support for alignment.
By default, this index relies on
scipy.spatial.KDTree
for fast lookup.Do not use
__init__()
directly. Instead usexarray.Dataset.set_xindex()
orxarray.DataArray.set_xindex()
to create and set the index from existing coordinates (see the example below).Examples
An example using a dataset with 2-dimensional coordinates.
>>> xx = [[1.0, 2.0], [3.0, 0.0]] >>> yy = [[11.0, 21.0], [29.0, 9.0]] >>> ds = xr.Dataset(coords={"xx": (("y", "x"), xx), "yy": (("y", "x"), yy)}) >>> ds <xarray.Dataset> Size: 64B Dimensions: (y: 2, x: 2) Coordinates: xx (y, x) float64 32B 1.0 2.0 3.0 0.0 yy (y, x) float64 32B 11.0 21.0 29.0 9.0 Dimensions without coordinates: y, x Data variables: *empty*
Creation of a NDPointIndex from the “xx” and “yy” coordinate variables:
>>> ds = ds.set_xindex(("xx", "yy"), xr.indexes.NDPointIndex) >>> ds <xarray.Dataset> Size: 64B Dimensions: (y: 2, x: 2) Coordinates: * xx (y, x) float64 32B 1.0 2.0 3.0 0.0 * yy (y, x) float64 32B 11.0 21.0 29.0 9.0 Dimensions without coordinates: y, x Data variables: *empty* Indexes: ┌ xx NDPointIndex (ScipyKDTreeAdapter) └ yy
Point-wise (nearest-neighbor) data selection using Xarray’s advanced indexing, i.e., using arbitrary dimension(s) for the Variable objects passed as labels:
>>> ds.sel( ... xx=xr.Variable("points", [1.9, 0.1]), ... yy=xr.Variable("points", [13.0, 8.0]), ... method="nearest", ... ) <xarray.Dataset> Size: 32B Dimensions: (points: 2) Coordinates: xx (points) float64 16B 1.0 0.0 yy (points) float64 16B 11.0 9.0 Dimensions without coordinates: points Data variables: *empty*
Data selection with scalar labels:
>>> ds.sel(xx=1.9, yy=13.0, method="nearest") <xarray.Dataset> Size: 16B Dimensions: () Coordinates: xx float64 8B 1.0 yy float64 8B 11.0 Data variables: *empty*
Data selection with broadcasting the input labels:
>>> ds.sel(xx=1.9, yy=xr.Variable("points", [13.0, 8.0]), method="nearest") <xarray.Dataset> Size: 32B Dimensions: (points: 2) Coordinates: xx (points) float64 16B 1.0 0.0 yy (points) float64 16B 11.0 9.0 Dimensions without coordinates: points Data variables: *empty*
>>> da = xr.DataArray( ... [[45.1, 53.3], [65.4, 78.2]], ... coords={"u": [1.9, 0.1], "v": [13.0, 8.0]}, ... dims=("u", "v"), ... ) >>> ds.sel(xx=da.u, yy=da.v, method="nearest") <xarray.Dataset> Size: 64B Dimensions: (u: 2, v: 2) Coordinates: xx (u, v) float64 32B 1.0 0.0 1.0 0.0 yy (u, v) float64 32B 11.0 9.0 11.0 9.0 Dimensions without coordinates: u, v Data variables: *empty*
Data selection with array-like labels (implicit dimensions):
>>> ds.sel(xx=[[1.9], [0.1]], yy=[[13.0], [8.0]], method="nearest") <xarray.Dataset> Size: 32B Dimensions: (y: 2, x: 1) Coordinates: xx (y, x) float64 16B 1.0 0.0 yy (y, x) float64 16B 11.0 9.0 Dimensions without coordinates: y, x Data variables: *empty*
Methods
__init__
(tree_obj, *, coord_names, dims, shape)concat
(indexes, dim[, positions])Create a new index by concatenating one or more indexes of the same type.
copy
([deep])Return a (deep) copy of this index.
create_variables
([variables])Maybe create new coordinate variables from this index.
equals
(other, *[, exclude])Compare this index with another index of the same type.
from_variables
(variables, *, options)Create a new index object from one or more coordinate variables.
isel
(indexers)Maybe returns a new index from the current index itself indexed by positional indexers.
join
(other[, how])Return a new index from the combination of this index with another index of the same type.
reindex_like
(other)Query the index with another index of the same type.
rename
(name_dict, dims_dict)Maybe update the index with new coordinate and dimension names.
roll
(shifts)Roll this index by an offset along one or more dimensions.
sel
(labels[, method, tolerance])Query the index with arbitrary coordinate label indexers.
should_add_coord_to_array
(name, var, dims)Define whether or not an index coordinate variable should be added to a new DataArray.
stack
(variables, dim)Create a new index by stacking coordinate variables into a single new dimension.
to_pandas_index
()Cast this xarray index to a pandas.Index object or raise a
TypeError
if this is not supported.unstack
()Unstack a (multi-)index into multiple (single) indexes.