diff --git a/xarray/core/indexes.py b/xarray/core/indexes.py index 844751f24bb..b66fbdf6504 100644 --- a/xarray/core/indexes.py +++ b/xarray/core/indexes.py @@ -231,9 +231,14 @@ def query(self, labels, method=None, tolerance=None): ) indexer = self.index.get_loc(label_value) else: - indexer = self.index.get_loc( - label_value, method=method, tolerance=tolerance - ) + if method is not None: + indexer = get_indexer_nd(self.index, label, method, tolerance) + if np.any(indexer < 0): + raise KeyError( + f"not all values found in index {coord_name!r}" + ) + else: + indexer = self.index.get_loc(label_value) elif label.dtype.kind == "b": indexer = label else: diff --git a/xarray/core/missing.py b/xarray/core/missing.py index 2525272f719..f3bb5351db5 100644 --- a/xarray/core/missing.py +++ b/xarray/core/missing.py @@ -564,9 +564,8 @@ def _localize(var, indexes_coords): minval = np.nanmin(new_x.values) maxval = np.nanmax(new_x.values) index = x.to_index() - imin = index.get_loc(minval, method="nearest") - imax = index.get_loc(maxval, method="nearest") - + imin = index.get_indexer([minval], method="nearest").item() + imax = index.get_indexer([maxval], method="nearest").item() indexes[dim] = slice(max(imin - 2, 0), imax + 2) indexes_coords[dim] = (x[indexes[dim]], new_x) return var.isel(**indexes), indexes_coords