Skip to content
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions xarray/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
get_valid_numpy_dtype,
is_duck_array,
is_duck_dask_array,
is_full_slice,
is_scalar,
is_valid_numpy_dtype,
to_0d_array,
Expand Down Expand Up @@ -298,17 +299,29 @@ def slice_slice(old_slice: slice, applied_slice: slice, size: int) -> slice:
return slice(start, stop, step)


def slice_slice_by_array(old_slice, array, size):
normalized = normalize_slice(old_slice, size)
new_indexer = array * normalized.step + normalized.start

if np.any(new_indexer >= normalized.stop):
raise IndexError("indices out of bounds") # TODO: more helpful error message

return new_indexer


def _index_indexer_1d(old_indexer, applied_indexer, size: int):
if isinstance(applied_indexer, slice) and applied_indexer == slice(None):
if is_full_slice(applied_indexer):
# shortcut for the usual case
return old_indexer
if isinstance(old_indexer, slice):
if isinstance(applied_indexer, slice):
indexer = slice_slice(old_indexer, applied_indexer, size)
elif isinstance(applied_indexer, integer_types):
indexer = range(*old_indexer.indices(size))[applied_indexer] # type: ignore[assignment]
elif is_full_slice(old_indexer):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this fix point (3) in #10311 (comment)

This is "combined" with the arrayized version of BasicIndexer(slice(None), slice(None), slice(None))

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, because for vectorized indexing we use _combine_indexers, not _index_indexer_1d (but see also my comment in #10311 about whether we can frame that particular case – fancy indexing along a single dimension – as an outer indexer)

indexer = applied_indexer
else:
indexer = _expand_slice(old_indexer, size)[applied_indexer]
indexer = slice_slice_by_array(old_indexer, applied_indexer, size)
else:
indexer = old_indexer[applied_indexer]
return indexer
Expand Down
Loading