Skip to content

Commit 49fbedc

Browse files
committed
cleanup
1 parent 6f0e753 commit 49fbedc

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
@@ -2984,14 +2984,6 @@ def isel(
29842984
all_keys = set(indexers.keys())
29852985

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

30193010
return self._construct_direct(
30203011
variables=variables,

0 commit comments

Comments
 (0)