Skip to content

Commit 8cce331

Browse files
committed
support passing tolerance but not method
1 parent 199765e commit 8cce331

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

xarray/core/indexes.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -573,18 +573,31 @@ def _query_slice(index, label, coord_name="", method=None, tolerance=None):
573573
"cannot use ``method`` argument with a slice object as an indexer and an index with non-unique values"
574574
)
575575

576-
slice_index_bounds = index.get_indexer(
577-
[slice_label_start, slice_label_stop], method=method, tolerance=tolerance
578-
)
576+
if method is None and tolerance is not None:
577+
# copies default behaviour of slicing with no tolerance, which is to be exclusive at both ends
578+
slice_index_start = index.get_indexer(
579+
[slice_label_start], method="backfill", tolerance=tolerance
580+
)
581+
slice_index_stop = index.get_indexer(
582+
[slice_label_stop], method="pad", tolerance=tolerance
583+
)
584+
else:
585+
# minor optimization to only issue a single `.get_indexer` call to get both start and end
586+
slice_index_start, slice_index_stop = index.get_indexer(
587+
[slice_label_start, slice_label_stop],
588+
method=method,
589+
tolerance=tolerance,
590+
)
579591

580-
if -1 in slice_index_bounds:
581-
# "no match" case - return empty slice
592+
if -1 in [slice_index_start, slice_index_stop]:
593+
# how pandas indicates the "no match" case - we return empty slice
582594
indexer = slice(0, 0)
583595
else:
584596
# +1 needed to emulate behaviour of xarray sel with slice without method kwarg, which is inclusive of point at stop label
597+
# assumes no duplicates, but we have forbidden that case above
585598
indexer = slice(
586-
slice_index_bounds[0].item(),
587-
slice_index_bounds[1].item() + 1,
599+
slice_index_start.item(),
600+
slice_index_stop.item() + 1,
588601
slice_index_step,
589602
)
590603
else:

xarray/tests/test_dataset.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2212,6 +2212,22 @@ def test_sel_method_with_slice(self) -> None:
22122212
)
22132213
assert_identical(expected, actual)
22142214

2215+
# test supposed default behaviour
2216+
expected = xr.Dataset(coords={"lat": ("lat", [21.1, 22.1])})
2217+
actual = data_float_coords.sel(lat=slice(21.0, 22.2))
2218+
assert_identical(expected, actual)
2219+
2220+
# tolerance specified but method not specified
2221+
expected = xr.Dataset(coords={"lat": ("lat", [21.1, 22.1])})
2222+
actual = data_float_coords.sel(
2223+
lat=slice(21.0, 22.2),
2224+
tolerance=1.0,
2225+
)
2226+
assert_identical(expected, actual)
2227+
# test this matches default behaviour without tolerance specified
2228+
default = data_float_coords.sel(lat=slice(21.0, 22.2))
2229+
assert_identical(default, actual)
2230+
22152231
# "no match" case - should return zero-size slice
22162232
expected = xr.Dataset(coords={"lat": ("lat", [])})
22172233
actual = data_float_coords.sel(

0 commit comments

Comments
 (0)