Skip to content

Commit abfeee6

Browse files
authored
Fix negative slice indexing when combined with int indexing (#935)
1 parent 6c4965b commit abfeee6

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

cubed/core/indexing.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,13 @@ def index(x, key):
4747

4848
# Use trick from xarray for negative step values
4949
where_negative_step = []
50+
num_integer_dims = 0
5051
for i, ia in enumerate(idx.args):
51-
if isinstance(ia, ndindex.Slice) and ia.step < 0:
52-
where_negative_step.append(i)
52+
if isinstance(ia, ndindex.Integer):
53+
num_integer_dims += 1
54+
elif isinstance(ia, ndindex.Slice) and ia.step < 0:
55+
# Axis in the output drops integer-indexed dimensions, so adjust
56+
where_negative_step.append(i - num_integer_dims)
5357
pos_slice = _convert_slice_with_negative_step(selection[i], x.shape[i])
5458
selection[i] = pos_slice
5559
where_negative_step = tuple(where_negative_step)

cubed/tests/test_array_api.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ def test_index_1d(spec, ind):
299299
(slice(None), slice(2, 4), xp.newaxis),
300300
(slice(None), 1),
301301
(1, slice(2, 4)),
302+
(0, slice(1, None, -1)),
302303
],
303304
)
304305
def test_index_2d(spec, ind):
@@ -375,6 +376,14 @@ def test_index_1d_step(spec, shape, chunks, ind, new_chunks_expected):
375376
(slice(14, 3, -2), slice(14, 3, -3)),
376377
((4, 2), (4,),),
377378
),
379+
# integer index on axis 0 drops a dimension; negative step on axis 1
380+
# must flip on axis 0 of the output, not axis 1 of the input
381+
(
382+
(1, 2),
383+
(1, 2),
384+
(0, slice(1, None, -1)),
385+
((2,),),
386+
),
378387
],
379388
)
380389
# fmt: on

0 commit comments

Comments
 (0)