@@ -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,24 @@ 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 ):
2997
+ var_dims = var .dims
2998
+ # Fastpath, skip all this metadata analysis for variables
2999
+ # with no dimensions
3000
+ if var_dims :
3001
+ # preserve variable order
3002
+ if name in index_variables :
3003
+ var = index_variables [name ]
3004
+ elif var_indexer_keys := all_keys .intersection (var_dims ):
3011
3005
var_indexers = {k : indexers [k ] for k in var_indexer_keys }
3012
3006
var = var .isel (var_indexers )
3013
3007
if drop and var .ndim == 0 and name in coord_names :
3014
3008
coord_names .remove (name )
3015
3009
continue
3016
3010
# Update our reference to `var_dims` after the call to isel
3011
+ # to reflect any dropped dimensions
3017
3012
var_dims = var .dims
3018
- dims .update (zip (var_dims , var .shape ))
3019
3013
variables [name ] = var
3014
+ dims .update (zip (var_dims , var .shape ))
3020
3015
3021
3016
return self ._construct_direct (
3022
3017
variables = variables ,
0 commit comments