(self)
| 3878 | assert ds.time.dtype == np.dtype("datetime64[s]") |
| 3879 | |
| 3880 | def test_set_index(self) -> None: |
| 3881 | expected = create_test_multiindex() |
| 3882 | mindex = expected["x"].to_index() |
| 3883 | indexes = [mindex.get_level_values(str(n)) for n in mindex.names] |
| 3884 | coords = {idx.name: ("x", idx) for idx in indexes} |
| 3885 | ds = Dataset({}, coords=coords) |
| 3886 | |
| 3887 | obj = ds.set_index(x=mindex.names) |
| 3888 | assert_identical(obj, expected) |
| 3889 | |
| 3890 | # ensure pre-existing indexes involved are removed |
| 3891 | # (level_2 should be a coordinate with no index) |
| 3892 | ds = create_test_multiindex() |
| 3893 | coords = {"x": coords["level_1"], "level_2": coords["level_2"]} |
| 3894 | expected = Dataset({}, coords=coords) |
| 3895 | |
| 3896 | obj = ds.set_index(x="level_1") |
| 3897 | assert_identical(obj, expected) |
| 3898 | |
| 3899 | # ensure set_index with no existing index and a single data var given |
| 3900 | # doesn't return multi-index |
| 3901 | ds = Dataset(data_vars={"x_var": ("x", [0, 1, 2])}) |
| 3902 | expected = Dataset(coords={"x": [0, 1, 2]}) |
| 3903 | assert_identical(ds.set_index(x="x_var"), expected) |
| 3904 | |
| 3905 | with pytest.raises(ValueError, match=r"bar variable\(s\) do not exist"): |
| 3906 | ds.set_index(foo="bar") |
| 3907 | |
| 3908 | with pytest.raises(ValueError, match=r"dimension mismatch.*"): |
| 3909 | ds.set_index(y="x_var") |
| 3910 | |
| 3911 | ds = Dataset(coords={"x": 1}) |
| 3912 | with pytest.raises( |
| 3913 | ValueError, match=r".*cannot set a PandasIndex.*scalar variable.*" |
| 3914 | ): |
| 3915 | ds.set_index(x="x") |
| 3916 | |
| 3917 | def test_set_index_deindexed_coords(self) -> None: |
| 3918 | # test de-indexed coordinates are converted to base variable |
nothing calls this directly
no test coverage detected