From f7945a311bb339045af9736216c5d42d77ecdf91 Mon Sep 17 00:00:00 2001 From: Deepak Cherian Date: Fri, 21 Jun 2024 14:38:03 -0600 Subject: [PATCH] cleanup --- xarray/core/dataset.py | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 0e16d723eb4..34acac1514f 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2986,14 +2986,6 @@ def isel( all_keys = set(indexers.keys()) for name, var in self._variables.items(): - # preserve variable order - if name in index_variables: - var = index_variables[name] - dims.update(zip(var.dims, var.shape)) - # Fastpath, skip all this metadata analysis for variables - # with no dimensions - # Keep the result of var.dims cached for future accesss to it - # # Optimization Note from hmaarrfk - 2024/06 # https://github.com/pydata/xarray/pull/9003#discussion_r1592767493 # It was found that accessing var.dims is faster than @@ -3002,21 +2994,24 @@ def isel( # By using var.dims, we can avoid the cost of resolving the # underlying array's shape and ndim since the dims are already # cached by the Variable - elif len(var_dims := var.dims): - # Large datasets with alot of metadata may have many scalars - # without any relevant dimensions for slicing. - # Pick those out quickly and avoid paying the cost below - # of resolving the var_indexers variables - if var_indexer_keys := all_keys.intersection(var_dims): + var_dims = var.dims + # Fastpath, skip all this metadata analysis for variables + # with no dimensions + if var_dims: + # preserve variable order + if name in index_variables: + var = index_variables[name] + elif var_indexer_keys := all_keys.intersection(var_dims): var_indexers = {k: indexers[k] for k in var_indexer_keys} var = var.isel(var_indexers) if drop and var.ndim == 0 and name in coord_names: coord_names.remove(name) continue # Update our reference to `var_dims` after the call to isel + # to reflect any dropped dimensions var_dims = var.dims - dims.update(zip(var_dims, var.shape)) variables[name] = var + dims.update(zip(var_dims, var.shape)) return self._construct_direct( variables=variables,