Skip to content

fix: return 0-D array for full reductions per Array API standard#932

Open
Abineshabee wants to merge 2 commits intopydata:mainfrom
Abineshabee:fix/array-api-reduction-returns-0d-array
Open

fix: return 0-D array for full reductions per Array API standard#932
Abineshabee wants to merge 2 commits intopydata:mainfrom
Abineshabee:fix/array-api-reduction-returns-0d-array

Conversation

@Abineshabee
Copy link

Fixes #921

Problem

sparse.sum() and all other full reductions (max, min, prod, mean, any, all, einsum) were returning a NumPy scalar instead of a 0-D array, violating the Array API standard.

Root Cause

SparseArray.reduce() had if out.ndim == 0: return out[()] which unwrapped the 0-D COO into a scalar. _einsum_single() also had two explicit scalar return paths.

Changes

  • _sparse_array.py — remove scalar unwrap in reduce(), unify mean() path
  • _common.py — fix both scalar paths in _einsum_single()
  • test_einsum.py — update scalar-output branches to use .todense()
  • test_array_function.py — add TestArrayAPIReductions with 16 regression tests

Testing

6079 passed, 0 failed

Abineshabee and others added 2 commits March 22, 2026 10:47
Fixes pydata#921

- Remove if out.ndim == 0: return out[()] in SparseArray.reduce()
  which was converting 0-D COO arrays into NumPy scalars
- Fix mean() to handle 0-D result uniformly without scalar branch
- Fix _einsum_single() two scalar escape hatches to return 0-D COO
- Update 	est_einsum.py scalar branches to use .todense()
- Add TestArrayAPIReductions with 16 regression tests
@github-actions github-actions bot added the fix label Mar 22, 2026
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.

array API standard: return an array for sparse.sum of a 2-D array

1 participant