Skip to content

fix: put new axes in the right place when indexing GCXS after an integer index - #983

Merged
hameerabbasi merged 1 commit into
pydata:mainfrom
raashish1601:fix/gcxs-getitem-none-after-int
Oct 9, 2026
Merged

hameerabbasi merged 1 commit into
pydata:mainfrom
raashish1601:fix/gcxs-getitem-none-after-int

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.

Indexing a GCXS array with None after an integer index puts the new axis in the wrong place, or fails:

x = np.arange(60).reshape(3, 4, 5)
s = sparse.GCXS.from_numpy(x)
s[0, None].shape        # (4, 1, 5), NumPy gives (1, 4, 5)
s[1, None, 2].shape     # (5, 1), NumPy gives (1, 5)

s2 = sparse.GCXS.from_numpy(np.arange(20).reshape(4, 5))
s2[0, None].shape       # (5, 1), NumPy gives (1, 5)
s2[None, 0].todense()   # numba TypingError in uncompress_dimension (indptr is None)
s2[1, None, 2]          # IndexError

getitem re-inserts the None axes at their position in the key, but integer indices drop an axis, so the position in the result is the number of non-integer entries before it. That count is now used. Two more cases are handled: when the result before adding the new axes is 1-d it has no indptr, so the new axes are added by reshaping that 1-d GCXS array instead of building a 2-d one without an index pointer; and a key that is all integers plus None (e.g. s2[1, None, 2]) now returns the element in an array of shape (1,), (1, 1), ... instead of failing.

Tests: added the new cases to test_slicing (3-d, all compressed axes) and a test_slicing_new_axes_2d test. 46 of the slicing/advanced_indexing cases in test_compressed.py fail on main and all pass here. A comparison with NumPy over ~11,000 keys built from None, integers, slices, a list and Ellipsis on 2-d and 3-d arrays with every choice of compressed axes has no mismatches, apart from the separate case where NumPy moves the advanced-index axis to the front because an integer and a list are separated by a slice or None (COO behaves the same way there; not changed here). test_compressed.py, test_compressed_2d.py, test_compressed_slicing.py, test_compressed_convert.py and test_array_function.py pass (1900 passed), and pre-commit (ruff check/format) is clean.

@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 d632550 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