Commit 8ec4d91
committed
Fix comparison with NumPy of slicing with negative step
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])1 parent 9447328 commit 8ec4d91
1 file changed
+3
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
111 | 111 | | |
112 | 112 | | |
113 | 113 | | |
114 | | - | |
| 114 | + | |
115 | 115 | | |
116 | 116 | | |
117 | 117 | | |
| |||
246 | 246 | | |
247 | 247 | | |
248 | 248 | | |
249 | | - | |
250 | | - | |
| 249 | + | |
| 250 | + | |
251 | 251 | | |
252 | 252 | | |
253 | 253 | | |
| |||
0 commit comments