Skip to content

fix: move the advanced-index axis to the front like NumPy when advanced indices are separated - #984

Merged
hameerabbasi merged 1 commit into
pydata:mainfrom
raashish1601:fix/advanced-index-axis-order
Oct 9, 2026
Merged

hameerabbasi merged 1 commit into
pydata:mainfrom
raashish1601:fix/advanced-index-axis-order

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 indexing against NumPy)
  • Closes #

Checklist

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

Please explain your changes below.

When the advanced indices in a key (arrays, and integers used together with them) are not next to each other, NumPy puts the axis they produce first. COO keeps it where the first array was, so the result has the wrong shape:

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

A small advanced_index_axis() helper in _slicing.py finds these keys. It checks adjacency on the index as given, since an Ellipsis separates advanced indices in NumPy even when it expands to no axes, and returns where getitem put the advanced axis. COO.__getitem__ then moves that axis to the front with a transpose. Keys whose advanced indices are adjacent (the common case) are unchanged.

Tests: added the cases above (and a boolean-mask variant) to test_advanced_indexing. A comparison with NumPy over ~28,000 keys built from None, integers, slices, two lists and Ellipsis on 2-d, 3-d and 4-d arrays had 4679 mismatches on main and none here. GCXS has the same behavior; I left it for a follow-up because #983 changes the same part of the GCXS indexing code. test_coo.py, test_compressed.py, test_array_function.py and test_dask_interop.py pass (4710 passed), and pre-commit (ruff check/format) is clean.

@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').

@rmz-oz rmz-oz left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I compared COO against numpy on 1203 keys of up to 3 entries built from ints, negative ints, np.int64, lists, int and bool arrays, step and reversed slices, None and Ellipsis on a 3d array: 30 mismatches on main, 0 here, and test_coo.py passes. fwiw a bool scalar key like s[True, :, [0, 2]] still comes out silently wrong ((2, 4) vs numpy's (2, 3, 5)), but it was already off on 0.19.2, so probably a separate issue.

@hameerabbasi
hameerabbasi merged commit 5b3dafe 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.

3 participants