slicing a slice with an array without expanding the slice#10580
Merged
keewis merged 17 commits intopydata:mainfrom Aug 12, 2025
Merged
slicing a slice with an array without expanding the slice#10580keewis merged 17 commits intopydata:mainfrom
keewis merged 17 commits intopydata:mainfrom
Conversation
dcherian
reviewed
Jul 28, 2025
xarray/core/indexing.py
Outdated
| 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): |
Contributor
There was a problem hiding this comment.
Does this fix point (3) in #10311 (comment)
This is "combined" with the arrayized version of BasicIndexer(slice(None), slice(None), slice(None))
Collaborator
Author
There was a problem hiding this comment.
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)
dcherian
reviewed
Jul 28, 2025
dcherian
reviewed
Jul 28, 2025
Co-authored-by: Deepak Cherian <dcherian@users.noreply.github.com>
dcherian
approved these changes
Jul 29, 2025
Contributor
dcherian
left a comment
There was a problem hiding this comment.
Thanks! A peakmem benchmark would be nice but I think our infra is broken at the moment.
shoyer
reviewed
Jul 30, 2025
This was referenced Nov 24, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The lazy indexing machinery currently uses
_index_indexer_1dto 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_slicewhen indexing aslicewith 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
stepis negative or positive, but we do have to raise anIndexErrorif there's any value>= size.