Bug: slices with a negative step and a negative stop return wrong elements
Maintainers usually reply within 2 days
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 75/100
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
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from pydata/sparse
-
Bug: full einsum contractions upcast the dtype and count booleansPossibly taken @RaphaelFakhri claimed this 17 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 2 days
-
BUG: ufunc outer rejects Python scalar and sequence operandsPossibly taken @nawaaaaaAaar claimed this 29 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 2 days
-
BUG: matmul fails with zero-length broadcast batch dimensionsPossibly taken @nawaaaaaAaar claimed this 29 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 40/100
Maintainers usually reply within 2 days
-
BUG: zero-length tensor contractions ignore requested output typesPossibly taken @nawaaaaaAaar claimed this 29 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 35/100
Maintainers usually reply within 2 days
-
BUG: inferred COO reshape dimensions lose integer precisionPossibly taken @nawaaaaaAaar claimed this 29 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 25/100
Maintainers usually reply within 2 days
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
abetlen/llama-cpp-python#2424 ·
Maintainers usually reply within 1 day
-
Bug IronPython Python 3
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
pyrevitlabs/pyRevit#4184 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 74/100
jqueryscript/awesome-claude-code#1009 ·
-
tests
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
Maintainers usually reply within 3 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day