-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
slicing a slice with an array without expanding the slice #10580
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
xarray/core/indexing.py
Outdated
# 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): |
There was a problem hiding this comment.
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))
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! A peakmem
benchmark would be nice but I think our infra is broken at the moment.
The lazy indexing machinery currently uses
_index_indexer_1d
to keep indexing lazy (for orthogonal indexing only), which tries very hard to combine multiple sequential indexers into a single indexer.However, it currently uses
_expand_slice
when indexing aslice
with an array, which has the potential to run out of memory for very large dimension sizes of the indexed array.Instead of materializing, we can just combine the slice (
s
) with the array (idx
) by:Interestingly, this works regardless of whether
step
is negative or positive, but we do have to raise anIndexError
if there's any value>= size
.