@@ -2986,14 +2986,6 @@ def isel(
2986
2986
all_keys = set (indexers .keys ())
2987
2987
2988
2988
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
- #
2997
2989
# Optimization Note from hmaarrfk - 2024/06
2998
2990
# https://github.com/pydata/xarray/pull/9003#discussion_r1592767493
2999
2991
# It was found that accessing var.dims is faster than
@@ -3002,21 +2994,20 @@ def isel(
3002
2994
# By using var.dims, we can avoid the cost of resolving the
3003
2995
# underlying array's shape and ndim since the dims are already
3004
2996
# 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
3019
3009
variables [name ] = var
3010
+ dims .update (zip (var .dims , var .shape ))
3020
3011
3021
3012
return self ._construct_direct (
3022
3013
variables = variables ,
0 commit comments