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

Function test_range_index_sel

xarray/tests/test_range_index.py:225–264  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

223
224
225def test_range_index_sel() -> None:
226 ds = create_dataset_arange(0.0, 1.0, 0.1)
227
228 # start-stop slice
229 actual = ds.sel(x=slice(0.12, 0.28), method="nearest")
230 expected = create_dataset_arange(0.1, 0.3, 0.1)
231 assert_identical(actual, expected, check_default_indexes=False, check_indexes=True)
232
233 # start-stop-step slice
234 actual = ds.sel(x=slice(0.0, 1.0, 0.2), method="nearest")
235 expected = ds.isel(x=range(0, 10, 2))
236 assert_identical(actual, expected, check_default_indexes=False, check_indexes=True)
237
238 # basic indexing
239 actual = ds.sel(x=0.52, method="nearest")
240 expected = xr.Dataset(coords={"x": 0.5})
241 assert_allclose(actual, expected)
242
243 actual = ds.sel(x=0.58, method="nearest")
244 expected = xr.Dataset(coords={"x": 0.6})
245 assert_allclose(actual, expected)
246
247 # 1-d array indexing
248 actual = ds.sel(x=[0.52, 0.58], method="nearest")
249 expected = xr.Dataset(coords={"x": [0.5, 0.6]})
250 assert_allclose(actual, expected)
251
252 actual = ds.sel(x=xr.Variable("y", [0.52, 0.58]), method="nearest")
253 expected = xr.Dataset(coords={"x": ("y", [0.5, 0.6])}).set_xindex("x")
254 assert_allclose(actual, expected, check_default_indexes=False)
255
256 actual = ds.sel(x=xr.DataArray([0.52, 0.58], dims="y"), method="nearest")
257 expected = xr.Dataset(coords={"x": ("y", [0.5, 0.6])}).set_xindex("x")
258 assert_allclose(actual, expected, check_default_indexes=False)
259
260 with pytest.raises(ValueError, match=r"RangeIndex only supports.*method.*nearest"):
261 ds.sel(x=0.1)
262
263 with pytest.raises(ValueError, match=r"RangeIndex doesn't support.*tolerance"):
264 ds.sel(x=0.1, method="nearest", tolerance=1e-3)
265
266
267def test_range_index_to_pandas_index() -> None:

Callers

nothing calls this directly

Calls 6

assert_identicalFunction · 0.90
assert_allcloseFunction · 0.90
create_dataset_arangeFunction · 0.85
selMethod · 0.45
iselMethod · 0.45
set_xindexMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…