MCPcopy
hub / github.com/pydata/xarray / sel

Method sel

xarray/indexes/nd_point_index.py:327–374  ·  view source on GitHub ↗
(
        self, labels: dict[Any, Any], method=None, tolerance=None
    )

Source from the content-addressed store, hash-verified

325 return dim_indexers
326
327 def sel(
328 self, labels: dict[Any, Any], method=None, tolerance=None
329 ) -> IndexSelResult:
330 if method != "nearest":
331 raise ValueError(
332 "NDPointIndex only supports selection with method='nearest'"
333 )
334
335 missing_labels = set(self._coord_names) - set(labels)
336 if missing_labels:
337 missing_labels_str = ",".join([f"{name}" for name in missing_labels])
338 raise ValueError(f"missing labels for coordinate(s): {missing_labels_str}.")
339
340 # maybe convert labels into xarray DataArray objects
341 xr_labels: dict[Any, DataArray] = {}
342
343 for name, lbl in labels.items():
344 if isinstance(lbl, DataArray):
345 xr_labels[name] = lbl
346 elif isinstance(lbl, Variable):
347 xr_labels[name] = DataArray(lbl)
348 elif is_scalar(lbl):
349 xr_labels[name] = DataArray(lbl, dims=())
350 elif np.asarray(lbl).ndim == len(self._dims):
351 xr_labels[name] = DataArray(lbl, dims=self._dims)
352 else:
353 raise ValueError(
354 "invalid label value. NDPointIndex only supports advanced (point-wise) indexing "
355 "with the following label value kinds:\n"
356 "- xarray.DataArray or xarray.Variable objects\n"
357 "- scalar values\n"
358 "- unlabelled array-like objects with the same number of dimensions "
359 f"than the {self._coord_names} coordinate variables ({len(self._dims)})"
360 )
361
362 # broadcast xarray labels against one another and determine labels shape and dimensions
363 broadcasted = broadcast(*xr_labels.values())
364 label_dims = broadcasted[0].dims
365 label_shape = broadcasted[0].shape
366 xr_labels = dict(zip(xr_labels, broadcasted, strict=True))
367
368 # get and return dimension indexers
369 points = get_points(xr_labels[name] for name in self._coord_names)
370 _, indices = self._tree_obj.query(points)
371
372 dim_indexers = self._get_dim_indexers(indices, label_dims, label_shape)
373
374 return IndexSelResult(dim_indexers=dim_indexers)
375
376 def rename(
377 self,

Callers

nothing calls this directly

Calls 10

_get_dim_indexersMethod · 0.95
DataArrayClass · 0.90
is_scalarFunction · 0.90
broadcastFunction · 0.90
IndexSelResultClass · 0.90
get_pointsFunction · 0.85
itemsMethod · 0.80
joinMethod · 0.45
valuesMethod · 0.45
queryMethod · 0.45

Tested by

no test coverage detected