Bug: slices with a negative step and a negative stop return wrong elements

Open
#1,019 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

@JoeyTan21 is already working on this.

Since Nov 1, 2026.

  • #1020 by @JoeyTan21 — open

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
75/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
numpy, python
Domain
data

Research direction

Start with normalize_index in sparse/numba_backend/_slicing.py and DOK._setitem; compare how negative-step slices are normalized and applied. Reproduce the examples in the issue for COO, GCXS and DOK, then add regression coverage for the reported slicing and assignment cases. Done when results match NumPy for those cases and the broader slice comparison no longer shows these discrepancies.

Written by the indexing model from the issue text.

Description

sparse version checks
  • I checked that this issue has not been reported before.
  • I confirmed this bug exists on the main branch of sparse (95f0da5).
Describe the bug

Basic slicing of COO, GCXS and DOK arrays returns the wrong elements when the slice has a negative step and a negative stop that is greater than or equal to the step, for example x[👎-1] or x[:-2:-2]. No error is raised. NumPy returns an empty array for x[👎-1], but sparse returns the whole axis reversed.

The cause is in normalize_index in sparse/numba_backend/_slicing.py. replace_none turns a missing stop of a negative-step slice into -1 (meaning past the start of the axis), and posify_index then tries to tell that -1 apart from a negative stop written by the caller with the check "not (0 > stop >= step)". Any caller-provided stop in that range is left unconverted.

DOK assignment has a related problem: DOK._setitem reads a start of 0 as a missing start (ind.start or shape - 1), so d[0::-1] = [9] writes 9 into every element.

Comparing against NumPy over 845 one-dimensional slices (start and stop in None and -8..8, step in -3, -2, -1, 1, 2), 66 differ for COO, 66 for GCXS, 96 for DOK getitem and 34 for DOK setitem.

Steps or code to reproduce the bug
import numpy as np
import sparse

x = np.arange(1, 6, dtype=float)
for fmt in ["coo", "gcxs"]:
    s = sparse.asarray(x, format=fmt)
    print(fmt, x[:-1:-1], s[:-1:-1].todense())
    print(fmt, x[:-2:-2], s[:-2:-2].todense())

d = sparse.DOK((5,))
d[0::-1] = [9.0]
print(d.todense())
Expected results
coo [] []
coo [5.] [5.]
gcxs [] []
gcxs [5.] [5.]
[9. 0. 0. 0. 0.]
Actual results
coo [] [5. 4. 3. 2. 1.]
coo [5.] [5. 3. 1.]
gcxs [] [5. 4. 3. 2. 1.]
gcxs [5.] [5. 3. 1.]
[9. 9. 9. 9. 9.]
Please describe your system.

macOS 15, Python 3.12, numpy 2.x, sparse main at 95f0da5 (0.19.4.dev1).

Possible fix

Resolve negative stops in replace_none before -1 takes on its sentinel meaning, and let DOK use the already normalized slice instead of normalizing twice and re-clipping. I have this with regression tests for COO, GCXS and DOK and can open a PR if that is welcome.

Disclosure: found, reproduced and the draft fix tested locally with the help of an AI coding agent (Claude Code); reviewed before filing.

Dominant language
Python
Stars
670
Forks
167
Avg merge
3d 4h
Merged PRs (30d)
7

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pydata/sparse

All issues in pydata/sparse

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.