Skip to content

fix: index GCXS arrays pointwise with several index arrays - #985

Merged
hameerabbasi merged 1 commit into
pydata:mainfrom
raashish1601:fix/gcxs-multiple-index-arrays
Oct 9, 2026
Merged

hameerabbasi merged 1 commit into
pydata:mainfrom
raashish1601:fix/gcxs-multiple-index-arrays

Conversation

@raashish1601

Copy link
Copy Markdown
Contributor

What type of PR is this? (check all applicable)

  • 💾 Refactor
  • 🪄 Feature
  • 🐞 Bug Fix
  • 🔧 Optimization
  • 📚 Documentation
  • 🧪 Test
  • 🛠️ Other

Related issues

  • Related issue # (none, found while comparing GCXS indexing against NumPy)
  • Closes #

Checklist

  • Code follows style guide
  • Tests added
  • Documented the changes

Please explain your changes below.

With more than one index array, GCXS returns the Cartesian product of the indices instead of selecting elements pointwise like NumPy and COO do:

x = np.arange(12).reshape(3, 4)
s = sparse.GCXS.from_numpy(x)
s[[0, 2], [1, 3]].todense()   # [[1, 3], [9, 11]], NumPy gives [1, 11]
s[[0, 1, 2], [0, 0, 0]].shape  # (3, 3), NumPy gives (3,)

The compressed selection in getitem builds the result shape from every index array separately (one axis each), which is outer indexing. Keys with two or more index arrays now go through COO indexing, which already implements NumPy's rules, and the result is converted back to GCXS. Keys with at most one index array take the same path as before, so their performance is unchanged. If you would rather have a native path for this case too, I can look into it; it needs a per-point search of the compressed rows instead of the current row/column product.

Tests: added several multi-array keys to test_advanced_indexing in test_compressed.py (all compressed axes). 30 of the new cases fail on main and all pass here. test_compressed.py, test_compressed_2d.py and test_compressed_slicing.py pass (1739 passed), and pre-commit (ruff check/format) is clean. Keys where an integer and an array are separated by a slice still follow COO's current axis order, which #984 changes to match NumPy.

@github-actions github-actions Bot added the fix label Oct 7, 2026
@raashish1601

Copy link
Copy Markdown
Contributor Author

codecov/project fails on every PR (target 100%; codecov/patch passes here), and the benchmarks job fails on main too (module 'finch' has no attribute 'Reflector').

@hameerabbasi
hameerabbasi merged commit 1bd427c into pydata:main Oct 9, 2026
12 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants