Fix comparison with NumPy of slicing with negative step#1319
Merged
nilgoyette merged 1 commit intorust-ndarray:masterfrom Aug 6, 2023
Merged
Fix comparison with NumPy of slicing with negative step#1319nilgoyette merged 1 commit intorust-ndarray:masterfrom
nilgoyette merged 1 commit intorust-ndarray:masterfrom
Conversation
The existing documentation states that the behavior of slicing is the same as in NumPy except when `step < -1`, implying that the behavior is the same when `step = -1`. But this is not true: In [1]: import numpy as np In [2]: x = np.arange(10) In [3]: x[2 : 5 : -1] # Analogous slice in `ndarray`: `array![4, 3, 2]` Out[3]: array([], dtype=int32) In [4]: x[5 : 2 : -1] # Analogous slice in `ndarray`: `array![]` Out[4]: array([5, 4, 3]) So `step < -1` should be replaced by `step < 0` in the documentation. There are some further differences in slicing behavior with negative step, having to do with the default values for `start` and `end`: In [5]: x[: 7 : -1] # Analogous slice in `ndarray`: `array![6, 5, 4, 3, 2, 1, 0]` Out[5]: array([9, 8]) In [6]: x[7 : : -1] # Analogous slice in `ndarray`: `array![9, 8, 7]` Out[6]: array([7, 6, 5, 4, 3, 2, 1, 0])
Collaborator
|
Thank you @venkat0791 for fixing this wrong information. The CI tests are failing, but it's not related to this MR so I'll merge anyway. I don't remember using negative slicing in ndarray so I learned something :) |
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 existing documentation states that the behavior of slicing is the same as in NumPy except when
step < -1, implying that the behavior is the same whenstep = -1. But this is not true:In [1]: import numpy as np
In [2]: x = np.arange(10)
In [3]: x[2 : 5 : -1] # Analogous slice in
ndarray:array![4, 3, 2]Out[3]: array([], dtype=int32)
In [4]: x[5 : 2 : -1] # Analogous slice in
ndarray:array![]Out[4]: array([5, 4, 3])
So
step < -1should be replaced bystep < 0in the documentation.There are some further differences in slicing behavior with negative step, having to do with the default values for
startandend:In [5]: x[: 7 : -1] # Analogous slice in
ndarray:array![6, 5, 4, 3, 2, 1, 0]Out[5]: array([9, 8])
In [6]: x[7 : : -1] # Analogous slice in
ndarray:array![9, 8, 7]Out[6]: array([7, 6, 5, 4, 3, 2, 1, 0])