| 1144 | return cls(index, dim, level_coords_dtype=level_coords_dtype) |
| 1145 | |
| 1146 | def unstack(self) -> tuple[dict[Hashable, Index], pd.MultiIndex]: |
| 1147 | clean_index = remove_unused_levels_categories(self.index) |
| 1148 | |
| 1149 | if not clean_index.is_unique: |
| 1150 | raise ValueError( |
| 1151 | "Cannot unstack MultiIndex containing duplicates. Make sure entries " |
| 1152 | f"are unique, e.g., by calling ``.drop_duplicates('{self.dim}')``, " |
| 1153 | "before unstacking." |
| 1154 | ) |
| 1155 | |
| 1156 | new_indexes: dict[Hashable, Index] = {} |
| 1157 | for name, lev in zip(clean_index.names, clean_index.levels, strict=True): |
| 1158 | idx = PandasIndex( |
| 1159 | lev.copy(), name, coord_dtype=self.level_coords_dtype[name] |
| 1160 | ) |
| 1161 | new_indexes[name] = idx |
| 1162 | |
| 1163 | return new_indexes, clean_index |
| 1164 | |
| 1165 | @classmethod |
| 1166 | def from_variables_maybe_expand( |