| 1885 | ds.sel(ind="bar", tolerance="nearest") # type: ignore[arg-type] |
| 1886 | |
| 1887 | def test_categorical_index(self) -> None: |
| 1888 | cat = pd.CategoricalIndex( |
| 1889 | ["foo", "bar", "foo"], |
| 1890 | categories=["foo", "bar", "baz", "qux", "quux", "corge"], |
| 1891 | ) |
| 1892 | ds = xr.Dataset( |
| 1893 | {"var": ("cat", np.arange(3))}, |
| 1894 | coords={"cat": ("cat", cat), "c": ("cat", [0, 1, 1])}, |
| 1895 | ) |
| 1896 | # test slice |
| 1897 | actual1 = ds.sel(cat="foo") |
| 1898 | expected1 = ds.isel(cat=[0, 2]) |
| 1899 | assert_identical(expected1, actual1) |
| 1900 | # make sure the conversion to the array works |
| 1901 | actual2 = ds.sel(cat="foo")["cat"].values |
| 1902 | assert (actual2 == np.array(["foo", "foo"])).all() |
| 1903 | |
| 1904 | ds = ds.set_index(index=["cat", "c"]) |
| 1905 | actual3 = ds.unstack("index") |
| 1906 | assert actual3["var"].shape == (2, 2) |
| 1907 | |
| 1908 | def test_categorical_index_reindex(self) -> None: |
| 1909 | cat = pd.CategoricalIndex( |