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

Aperta
#1,019 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

@JoeyTan21 ci sta già lavorando.

Dal 1/11/2026.

  • #1020 di @JoeyTan21 — aperta

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
75/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
numpy, python
Ambito
data

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Python
Stelle
670
Fork
167
Merge medio
3g 4h
PR unite (30g)
7

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di pydata/sparse

Tutte le issue di pydata/sparse

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.