Skip to content

fix: move the advanced-index axis to the front for GCXS like NumPy - #990

Open
raashish1601 wants to merge 1 commit into
pydata:mainfrom
raashish1601:fix/gcxs-advanced-index-axis
Open

raashish1601 wants to merge 1 commit into
pydata:mainfrom
raashish1601:fix/gcxs-advanced-index-axis

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

Checklist

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

Please explain your changes below.

This is the GCXS follow-up mentioned in #984. When the advanced indices in a key are not next to each other, NumPy puts the axis they produce first. GCXS still keeps it in place, so the shape and values are wrong:

s = sparse.random((2, 3, 4), density=0.5, format="gcxs")
s[1, :, [0, 2]].shape          # (3, 2), NumPy gives (2, 3)
s[1, None, [0, 2]].shape       # (1, 2, 4), NumPy gives (2, 1, 4)
s[:, 1, ..., [0, 2]]           # right shape (2, 2) but transposed values

getitem in _compressed/indexing.py now uses the advanced_index_axis() helper from #984 and transposes the advanced axis to the front before returning, in both the reshape path for 1-d results and the general path. Keys with several index arrays already go through COO since #985, so they get the COO fix.

Tests: added the separated cases to test_advanced_indexing in test_compressed.py (16 of the new cases fail on main). A comparison with NumPy over ~37,000 keys built from None, integers, slices, one list and Ellipsis on 3-d and 4-d GCXS arrays with different compressed_axes had 3112 mismatches on main and none here. test_compressed.py passes and ruff check/format is clean.

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

Copy link
Copy Markdown
Contributor Author

codecov/project fails on every PR here (codecov/patch passes), and the benchmarks job fails on main too, so neither is from this change.

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.

1 participant