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