(self)
| 4145 | # along dimension + create_index=True |
| 4146 | |
| 4147 | def test_stack_multi_index(self) -> None: |
| 4148 | # multi-index on a dimension to stack is discarded too |
| 4149 | midx = pd.MultiIndex.from_product([["a", "b"], [0, 1]], names=("lvl1", "lvl2")) |
| 4150 | coords = Coordinates.from_pandas_multiindex(midx, "x") |
| 4151 | coords["y"] = [0, 1] |
| 4152 | ds = xr.Dataset( |
| 4153 | data_vars={"b": (("x", "y"), [[0, 1], [2, 3], [4, 5], [6, 7]])}, |
| 4154 | coords=coords, |
| 4155 | ) |
| 4156 | expected = Dataset( |
| 4157 | data_vars={"b": ("z", [0, 1, 2, 3, 4, 5, 6, 7])}, |
| 4158 | coords={ |
| 4159 | "x": ("z", np.repeat(midx.values, 2)), |
| 4160 | "lvl1": ("z", np.repeat(midx.get_level_values("lvl1"), 2)), |
| 4161 | "lvl2": ("z", np.repeat(midx.get_level_values("lvl2"), 2)), |
| 4162 | "y": ("z", [0, 1, 0, 1] * 2), |
| 4163 | }, |
| 4164 | ) |
| 4165 | actual = ds.stack(z=["x", "y"], create_index=False) |
| 4166 | assert_identical(expected, actual) |
| 4167 | assert len(actual.xindexes) == 0 |
| 4168 | |
| 4169 | with pytest.raises(ValueError, match=r"cannot create.*wraps a multi-index"): |
| 4170 | ds.stack(z=["x", "y"], create_index=True) |
| 4171 | |
| 4172 | def test_stack_non_dim_coords(self) -> None: |
| 4173 | ds = Dataset( |
nothing calls this directly
no test coverage detected