Apply an indexing operation over the subtree, handling missing dimensions and inherited coordinates gracefully by only applying indexing at each node selectively.
(
self,
func: Callable[[Dataset, Mapping[Any, Any]], Dataset],
indexers: Mapping[Any, Any],
missing_dims: ErrorOptionsWithWarn = "raise",
)
| 2252 | return type(self).from_dict(result, name=self.name) |
| 2253 | |
| 2254 | def _selective_indexing( |
| 2255 | self, |
| 2256 | func: Callable[[Dataset, Mapping[Any, Any]], Dataset], |
| 2257 | indexers: Mapping[Any, Any], |
| 2258 | missing_dims: ErrorOptionsWithWarn = "raise", |
| 2259 | ) -> Self: |
| 2260 | """Apply an indexing operation over the subtree, handling missing |
| 2261 | dimensions and inherited coordinates gracefully by only applying |
| 2262 | indexing at each node selectively. |
| 2263 | """ |
| 2264 | all_dims = self._get_all_dims() |
| 2265 | indexers = drop_dims_from_indexers(indexers, all_dims, missing_dims) |
| 2266 | |
| 2267 | result = {} |
| 2268 | for path, node in self.subtree_with_keys: |
| 2269 | node_indexers = {k: v for k, v in indexers.items() if k in node.dims} |
| 2270 | with add_path_context_to_errors(path): |
| 2271 | node_result = func(node.dataset, node_indexers) |
| 2272 | # Indexing datasets corresponding to each node results in redundant |
| 2273 | # coordinates when indexes from a parent node are inherited. |
| 2274 | # Ideally, we would avoid creating such coordinates in the first |
| 2275 | # place, but that would require implementing indexing operations at |
| 2276 | # the Variable instead of the Dataset level. |
| 2277 | if node is not self: |
| 2278 | for k in node_indexers: |
| 2279 | if k not in node._node_coord_variables and k in node_result.coords: |
| 2280 | # We remove all inherited coordinates. Coordinates |
| 2281 | # corresponding to an index would be de-duplicated by |
| 2282 | # _deduplicate_inherited_coordinates(), but indexing (e.g., |
| 2283 | # with a scalar) can also create scalar coordinates, which |
| 2284 | # need to be explicitly removed. |
| 2285 | del node_result.coords[k] |
| 2286 | result[path] = node_result |
| 2287 | return type(self).from_dict(result, name=self.name) |
| 2288 | |
| 2289 | def isel( |
| 2290 | self, |
no test coverage detected