Skip to content

Conversation

dschult
Copy link
Contributor

@dschult dschult commented Jun 22, 2024

Fixes #21016

Advanced indexing with both slice and array indices provides incorrect shapes for examples such as csr_array(np.arange(15).reshape((5, 3)))[:, [1, 2]]. This is due to a bug introduced in #20120 and caught via nightly builds.

This PR provides a test showing the problem (it copies a test function from test_base.py and adapts it to 2d sparse arrays). A fix is also included. The fix involves adding a case for when only one dimension's index-value is an array while the index also contains a slice in a separate dimension. The result should now mimic numpy's indexing behavior for advanced fancy indexing using both slices and arrays).

This unfortunate bug would have been easier to find (and the PR introducing it easier to review) if we converted the test_base.py module to directly and fully test sparray methods as well as spmatrix (as is developing in #20894 and #20895). In the meantime, this example shows the power of quick feedback based on nightly builds. And hopefully this PR allows the tests run on nightly builds to pass.

@dschult dschult requested a review from perimosocordiae as a code owner June 22, 2024 05:57
@github-actions github-actions bot added scipy.sparse defect A clear bug or issue that prevents SciPy from being installed or used as expected labels Jun 22, 2024
B[:, [-1, -3]][[2, -4], :]
)


Copy link
Member

Choose a reason for hiding this comment

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

Extra newline here

Co-authored-by: CJ Carey <perimosocordiae@gmail.com>
@perimosocordiae perimosocordiae merged commit 6d5ab4c into scipy:main Jun 24, 2024
@perimosocordiae
Copy link
Member

Thanks, merged.

@lucascolley lucascolley added this to the 1.15.0 milestone Jun 24, 2024
@dschult dschult deleted the fix_fancy_indexing branch July 8, 2024 21:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.sparse
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: Indexing broken for sparse arrays
3 participants