Prepare an indexing key for an indexing operation. Parameters ---------- key : int, slice, array-like, dict or tuple of integer, slice and array-like Any valid input for indexing. Returns ------- dims : tuple Dimension of the
(self, key)
| 613 | return key |
| 614 | |
| 615 | def _broadcast_indexes(self, key): |
| 616 | """Prepare an indexing key for an indexing operation. |
| 617 | |
| 618 | Parameters |
| 619 | ---------- |
| 620 | key : int, slice, array-like, dict or tuple of integer, slice and array-like |
| 621 | Any valid input for indexing. |
| 622 | |
| 623 | Returns |
| 624 | ------- |
| 625 | dims : tuple |
| 626 | Dimension of the resultant variable. |
| 627 | indexers : IndexingTuple subclass |
| 628 | Tuple of integer, array-like, or slices to use when indexing |
| 629 | self._data. The type of this argument indicates the type of |
| 630 | indexing to perform, either basic, outer or vectorized. |
| 631 | new_order : Optional[Sequence[int]] |
| 632 | Optional reordering to do on the result of indexing. If not None, |
| 633 | the first len(new_order) indexing should be moved to these |
| 634 | positions. |
| 635 | """ |
| 636 | key = self._item_key_to_tuple(key) # key is a tuple |
| 637 | # Fast path: key is already a tuple of the right length with only |
| 638 | # ints and slices (the common case from Variable.isel) |
| 639 | if ( |
| 640 | isinstance(key, tuple) |
| 641 | and len(key) == self.ndim |
| 642 | and all( |
| 643 | not isinstance(k, bool) and isinstance(k, BASIC_INDEXING_TYPES) |
| 644 | for k in key |
| 645 | ) |
| 646 | ): |
| 647 | return self._broadcast_indexes_basic(key) |
| 648 | |
| 649 | # key is a tuple of full size |
| 650 | key = indexing.expanded_indexer(key, self.ndim) |
| 651 | # Convert a scalar Variable to a 0d-array |
| 652 | key = tuple( |
| 653 | k.data if isinstance(k, Variable) and k.ndim == 0 else k for k in key |
| 654 | ) |
| 655 | # Convert a 0d numpy arrays to an integer |
| 656 | # dask 0d arrays are passed through |
| 657 | key = tuple( |
| 658 | k.item() if isinstance(k, np.ndarray) and k.ndim == 0 else k for k in key |
| 659 | ) |
| 660 | |
| 661 | if all( |
| 662 | (isinstance(k, BASIC_INDEXING_TYPES) and not isinstance(k, bool)) |
| 663 | for k in key |
| 664 | ): |
| 665 | return self._broadcast_indexes_basic(key) |
| 666 | |
| 667 | self._validate_indexers(key) |
| 668 | # Detect it can be mapped as an outer indexer |
| 669 | # If all key is unlabeled, or |
| 670 | # key can be mapped as an OuterIndexer. |
| 671 | if all(not isinstance(k, Variable) for k in key): |
| 672 | return self._broadcast_indexes_outer(key) |