Skip to content

Commit 372dd9e

Browse files
dcherianhmaarrfk
authored andcommitted
cleanup
1 parent 5bcf40c commit 372dd9e

File tree

1 file changed

+13
-22
lines changed

1 file changed

+13
-22
lines changed

xarray/core/dataset.py

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2986,14 +2986,6 @@ def isel(
29862986
all_keys = set(indexers.keys())
29872987

29882988
for name, var in self._variables.items():
2989-
# preserve variable order
2990-
if name in index_variables:
2991-
var = index_variables[name]
2992-
dims.update(zip(var.dims, var.shape))
2993-
# Fastpath, skip all this metadata analysis for variables
2994-
# with no dimensions
2995-
# Keep the result of var.dims cached for future accesss to it
2996-
#
29972989
# Optimization Note from hmaarrfk - 2024/06
29982990
# https://github.com/pydata/xarray/pull/9003#discussion_r1592767493
29992991
# It was found that accessing var.dims is faster than
@@ -3002,21 +2994,20 @@ def isel(
30022994
# By using var.dims, we can avoid the cost of resolving the
30032995
# underlying array's shape and ndim since the dims are already
30042996
# cached by the Variable
3005-
elif len(var_dims := var.dims):
3006-
# Large datasets with alot of metadata may have many scalars
3007-
# without any relevant dimensions for slicing.
3008-
# Pick those out quickly and avoid paying the cost below
3009-
# of resolving the var_indexers variables
3010-
if var_indexer_keys := all_keys.intersection(var_dims):
3011-
var_indexers = {k: indexers[k] for k in var_indexer_keys}
3012-
var = var.isel(var_indexers)
3013-
if drop and var.ndim == 0 and name in coord_names:
3014-
coord_names.remove(name)
3015-
continue
3016-
# Update our reference to `var_dims` after the call to isel
3017-
var_dims = var.dims
3018-
dims.update(zip(var_dims, var.shape))
2997+
var_dims = var.dims
2998+
# preserve variable order
2999+
if name in index_variables:
3000+
var = index_variables[name]
3001+
# Fastpath, skip all this metadata analysis for variables
3002+
# with no dimensions
3003+
elif var_dims and (var_indexer_keys := all_keys.intersection(var_dims)):
3004+
var_indexers = {k: indexers[k] for k in var_indexer_keys}
3005+
var = var.isel(var_indexers)
3006+
if drop and var.ndim == 0 and name in coord_names:
3007+
coord_names.remove(name)
3008+
continue
30193009
variables[name] = var
3010+
dims.update(zip(var.dims, var.shape))
30203011

30213012
return self._construct_direct(
30223013
variables=variables,

0 commit comments

Comments
 (0)