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

Method test_sel_dataarray_mindex

xarray/tests/test_dataset.py:1824–1868  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1822 assert np.allclose(actual["new_dim"].values, ind["new_dim"].values)
1823
1824 def test_sel_dataarray_mindex(self) -> None:
1825 midx = pd.MultiIndex.from_product([list("abc"), [0, 1]], names=("one", "two"))
1826 midx_coords = Coordinates.from_pandas_multiindex(midx, "x")
1827 midx_coords["y"] = range(3)
1828
1829 mds = xr.Dataset(
1830 {"var": (("x", "y"), np.random.rand(6, 3))}, coords=midx_coords
1831 )
1832
1833 actual_isel = mds.isel(x=xr.DataArray(np.arange(3), dims="x"))
1834 actual_sel = mds.sel(x=DataArray(midx[:3], dims="x"))
1835 assert actual_isel["x"].dims == ("x",)
1836 assert actual_sel["x"].dims == ("x",)
1837 assert_identical(actual_isel, actual_sel)
1838
1839 actual_isel = mds.isel(x=xr.DataArray(np.arange(3), dims="z"))
1840 actual_sel = mds.sel(x=Variable("z", midx[:3]))
1841 assert actual_isel["x"].dims == ("z",)
1842 assert actual_sel["x"].dims == ("z",)
1843 assert_identical(actual_isel, actual_sel)
1844
1845 # with coordinate
1846 actual_isel = mds.isel(
1847 x=xr.DataArray(np.arange(3), dims="z", coords={"z": [0, 1, 2]})
1848 )
1849 actual_sel = mds.sel(
1850 x=xr.DataArray(midx[:3], dims="z", coords={"z": [0, 1, 2]})
1851 )
1852 assert actual_isel["x"].dims == ("z",)
1853 assert actual_sel["x"].dims == ("z",)
1854 assert_identical(actual_isel, actual_sel)
1855
1856 # Vectorized indexing with level-variables raises an error
1857 with pytest.raises(ValueError, match=r"Vectorized selection is "):
1858 mds.sel(one=["a", "b"])
1859
1860 with pytest.raises(
1861 ValueError,
1862 match=r"Vectorized selection is not available along coordinate 'x' with a multi-index",
1863 ):
1864 mds.sel(
1865 x=xr.DataArray(
1866 [np.array(midx[:2]), np.array(midx[-2:])], dims=["a", "b"]
1867 )
1868 )
1869
1870 def test_sel_categorical(self) -> None:
1871 ind = pd.Series(["foo", "bar"], dtype="category")

Callers

nothing calls this directly

Calls 7

iselMethod · 0.95
selMethod · 0.95
DataArrayClass · 0.90
assert_identicalFunction · 0.90
VariableClass · 0.90
arangeMethod · 0.80

Tested by

no test coverage detected