|
66 | 66 | propagate_indexes,
|
67 | 67 | roll_index,
|
68 | 68 | )
|
| 69 | +from .indexing import is_fancy_indexer |
69 | 70 | from .merge import (
|
70 | 71 | dataset_merge_method,
|
71 | 72 | dataset_update_method,
|
|
78 | 79 | Default,
|
79 | 80 | Frozen,
|
80 | 81 | SortedKeysDict,
|
81 |
| - _default, |
82 | 82 | _check_inplace,
|
| 83 | + _default, |
83 | 84 | decode_numpy_dict_values,
|
84 | 85 | either_dict_or_kwargs,
|
85 | 86 | hashable,
|
@@ -1907,6 +1908,48 @@ def isel(
|
1907 | 1908 | DataArray.isel
|
1908 | 1909 | """
|
1909 | 1910 | indexers = either_dict_or_kwargs(indexers, indexers_kwargs, "isel")
|
| 1911 | + if any(is_fancy_indexer(idx) for idx in indexers.values()): |
| 1912 | + return self._isel_fancy(indexers, drop=drop) |
| 1913 | + |
| 1914 | + # Much faster algorithm for when all indexers are ints, slices, one-dimensional |
| 1915 | + # lists, or zero or one-dimensional np.ndarray's |
| 1916 | + invalid = indexers.keys() - self.dims.keys() |
| 1917 | + if invalid: |
| 1918 | + raise ValueError("dimensions %r do not exist" % invalid) |
| 1919 | + |
| 1920 | + variables = {} |
| 1921 | + dims: Dict[Hashable, Tuple[int, ...]] = {} |
| 1922 | + coord_names = self._coord_names.copy() |
| 1923 | + indexes = self._indexes.copy() if self._indexes is not None else None |
| 1924 | + |
| 1925 | + for var_name, var_value in self._variables.items(): |
| 1926 | + var_indexers = {k: v for k, v in indexers.items() if k in var_value.dims} |
| 1927 | + if var_indexers: |
| 1928 | + var_value = var_value.isel(var_indexers) |
| 1929 | + if drop and var_value.ndim == 0 and var_name in coord_names: |
| 1930 | + coord_names.remove(var_name) |
| 1931 | + if indexes: |
| 1932 | + indexes.pop(var_name, None) |
| 1933 | + continue |
| 1934 | + if indexes and var_name in indexes: |
| 1935 | + if var_value.ndim == 1: |
| 1936 | + indexes[var_name] = var_value.to_index() |
| 1937 | + else: |
| 1938 | + del indexes[var_name] |
| 1939 | + variables[var_name] = var_value |
| 1940 | + dims.update(zip(var_value.dims, var_value.shape)) |
| 1941 | + |
| 1942 | + return self._construct_direct( |
| 1943 | + variables=variables, |
| 1944 | + coord_names=coord_names, |
| 1945 | + dims=dims, |
| 1946 | + attrs=self._attrs, |
| 1947 | + indexes=indexes, |
| 1948 | + encoding=self._encoding, |
| 1949 | + file_obj=self._file_obj, |
| 1950 | + ) |
| 1951 | + |
| 1952 | + def _isel_fancy(self, indexers: Mapping[Hashable, Any], *, drop: bool) -> "Dataset": |
1910 | 1953 | # Note: we need to preserve the original indexers variable in order to merge the
|
1911 | 1954 | # coords below
|
1912 | 1955 | indexers_list = list(self._validate_indexers(indexers))
|
|
0 commit comments