Keep only the provided levels and return a new multi-index with its corresponding coordinates.
(
self, level_variables: Mapping[Any, Variable]
)
| 1220 | return obj, index_vars |
| 1221 | |
| 1222 | def keep_levels( |
| 1223 | self, level_variables: Mapping[Any, Variable] |
| 1224 | ) -> PandasMultiIndex | PandasIndex: |
| 1225 | """Keep only the provided levels and return a new multi-index with its |
| 1226 | corresponding coordinates. |
| 1227 | |
| 1228 | """ |
| 1229 | index = self.index.droplevel( |
| 1230 | [k for k in self.index.names if k not in level_variables] |
| 1231 | ) |
| 1232 | |
| 1233 | if isinstance(index, pd.MultiIndex): |
| 1234 | level_coords_dtype = {k: self.level_coords_dtype[k] for k in index.names} |
| 1235 | return self._replace(index, level_coords_dtype=level_coords_dtype) |
| 1236 | else: |
| 1237 | # backward compatibility: rename the level coordinate to the dimension name |
| 1238 | return PandasIndex( |
| 1239 | index.rename(self.dim), |
| 1240 | self.dim, |
| 1241 | coord_dtype=self.level_coords_dtype[index.name], |
| 1242 | ) |
| 1243 | |
| 1244 | def reorder_levels( |
| 1245 | self, level_variables: Mapping[Any, Variable] |
no test coverage detected