Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dcherian authored and hmaarrfk committed Jun 22, 2024
1 parent 5bcf40c commit f7945a3
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2986,14 +2986,6 @@ def isel(
all_keys = set(indexers.keys())

for name, var in self._variables.items():
# preserve variable order
if name in index_variables:
var = index_variables[name]
dims.update(zip(var.dims, var.shape))
# Fastpath, skip all this metadata analysis for variables
# with no dimensions
# Keep the result of var.dims cached for future accesss to it
#
# Optimization Note from hmaarrfk - 2024/06
# https://github.com/pydata/xarray/pull/9003#discussion_r1592767493
# It was found that accessing var.dims is faster than
Expand All @@ -3002,21 +2994,24 @@ def isel(
# By using var.dims, we can avoid the cost of resolving the
# underlying array's shape and ndim since the dims are already
# cached by the Variable
elif len(var_dims := var.dims):
# Large datasets with alot of metadata may have many scalars
# without any relevant dimensions for slicing.
# Pick those out quickly and avoid paying the cost below
# of resolving the var_indexers variables
if var_indexer_keys := all_keys.intersection(var_dims):
var_dims = var.dims
# Fastpath, skip all this metadata analysis for variables
# with no dimensions
if var_dims:
# preserve variable order
if name in index_variables:
var = index_variables[name]
elif var_indexer_keys := all_keys.intersection(var_dims):
var_indexers = {k: indexers[k] for k in var_indexer_keys}
var = var.isel(var_indexers)
if drop and var.ndim == 0 and name in coord_names:
coord_names.remove(name)
continue
# Update our reference to `var_dims` after the call to isel
# to reflect any dropped dimensions
var_dims = var.dims
dims.update(zip(var_dims, var.shape))
variables[name] = var
dims.update(zip(var_dims, var.shape))

return self._construct_direct(
variables=variables,
Expand Down

0 comments on commit f7945a3

Please sign in to comment.