(
self,
dim: Hashable,
index_and_vars: tuple[Index, dict[Hashable, Variable]],
fill_value,
sparse: bool,
)
| 5491 | ) |
| 5492 | |
| 5493 | def _unstack_full_reindex( |
| 5494 | self, |
| 5495 | dim: Hashable, |
| 5496 | index_and_vars: tuple[Index, dict[Hashable, Variable]], |
| 5497 | fill_value, |
| 5498 | sparse: bool, |
| 5499 | ) -> Self: |
| 5500 | index, index_vars = index_and_vars |
| 5501 | variables: dict[Hashable, Variable] = {} |
| 5502 | indexes = {k: v for k, v in self._indexes.items() if k != dim} |
| 5503 | |
| 5504 | new_indexes, clean_index = index.unstack() |
| 5505 | indexes.update(new_indexes) |
| 5506 | |
| 5507 | new_index_variables = {} |
| 5508 | for idx in new_indexes.values(): |
| 5509 | new_index_variables.update(idx.create_variables(index_vars)) |
| 5510 | |
| 5511 | new_dim_sizes = {k: v.size for k, v in new_index_variables.items()} |
| 5512 | variables.update(new_index_variables) |
| 5513 | |
| 5514 | # take a shortcut in case the MultiIndex was not modified. |
| 5515 | full_idx = pd.MultiIndex.from_product( |
| 5516 | clean_index.levels, names=clean_index.names |
| 5517 | ) |
| 5518 | if clean_index.equals(full_idx): |
| 5519 | obj = self |
| 5520 | else: |
| 5521 | # TODO: we may depreciate implicit re-indexing with a pandas.MultiIndex |
| 5522 | xr_full_idx = PandasMultiIndex(full_idx, dim) |
| 5523 | indexers = Indexes( |
| 5524 | dict.fromkeys(index_vars, xr_full_idx), |
| 5525 | xr_full_idx.create_variables(index_vars), |
| 5526 | ) |
| 5527 | obj = self._reindex( |
| 5528 | indexers, copy=False, fill_value=fill_value, sparse=sparse |
| 5529 | ) |
| 5530 | |
| 5531 | for name, var in obj.variables.items(): |
| 5532 | if name not in index_vars: |
| 5533 | if dim in var.dims: |
| 5534 | variables[name] = var.unstack({dim: new_dim_sizes}) |
| 5535 | else: |
| 5536 | variables[name] = var |
| 5537 | |
| 5538 | coord_names = set(self._coord_names) - {dim} | set(new_dim_sizes) |
| 5539 | |
| 5540 | return self._replace_with_new_dims( |
| 5541 | variables, coord_names=coord_names, indexes=indexes |
| 5542 | ) |
| 5543 | |
| 5544 | def unstack( |
| 5545 | self, |
no test coverage detected