splits dataarray along dimension 'dim
(self, dim: Hashable)
| 595 | return self._replace(variable, coords, name, indexes=indexes) |
| 596 | |
| 597 | def _to_dataset_split(self, dim: Hashable) -> Dataset: |
| 598 | """splits dataarray along dimension 'dim'""" |
| 599 | |
| 600 | def subset(dim, label): |
| 601 | array = self.loc[{dim: label}] |
| 602 | array.attrs = {} |
| 603 | return as_variable(array) |
| 604 | |
| 605 | variables_from_split = { |
| 606 | label: subset(dim, label) for label in self.get_index(dim) |
| 607 | } |
| 608 | coord_names = set(self._coords) - {dim} |
| 609 | |
| 610 | ambiguous_vars = set(variables_from_split) & coord_names |
| 611 | if ambiguous_vars: |
| 612 | rename_msg_fmt = ", ".join([f"{v}=..." for v in sorted(ambiguous_vars)]) |
| 613 | raise ValueError( |
| 614 | f"Splitting along the dimension {dim!r} would produce the variables " |
| 615 | f"{tuple(sorted(ambiguous_vars))} which are also existing coordinate " |
| 616 | f"variables. Use DataArray.rename({rename_msg_fmt}) or " |
| 617 | f"DataArray.assign_coords({dim}=...) to resolve this ambiguity." |
| 618 | ) |
| 619 | |
| 620 | variables = variables_from_split | { |
| 621 | k: v for k, v in self._coords.items() if k != dim |
| 622 | } |
| 623 | indexes = filter_indexes_from_coords(self._indexes, coord_names) |
| 624 | dataset = Dataset._construct_direct( |
| 625 | variables, coord_names, indexes=indexes, attrs=self.attrs |
| 626 | ) |
| 627 | return dataset |
| 628 | |
| 629 | def _to_dataset_whole( |
| 630 | self, name: Hashable = None, shallow_copy: bool = True |
no test coverage detected