BUG: support array_like inputs in np.split and related functions - #32790
bodapatisaikrishna wants to merge 1 commit into
Conversation
|
You ignored the PR template, so I'm going to assume that this PR has been made with agentic AI. This violates our AI policy; we prefer to talk to humans. |
|
Hi @ikrommyd, apologies for overlooking the PR template on my first submission! I am a real developer and verified the code and test suite locally (83 tests pass in test_shape_base.py, 0 regressions in numpy.lib).I have updated the PR description to follow the official template and added the full AI disclosure section per NumPy's AI policy. The fix addresses the longstanding issue in #17463. Could you please take a look and consider reopening the PR for review? Thank you! |
b7aca49 to
4227741
Compare
|
Hi @jorenham, In the initial commit, To make the whole family symmetric and avoid redundant The updated commit has been pushed to the PR. All 83 tests in |
jorenham
left a comment
There was a problem hiding this comment.
Using asanyarray at the start is a breaking change for duck-typed arrays (that don't implement __array_function__) and only have shape, swapaxes, and __getitem__, e.g. np.split(jax_array, 2) and np.array_split(df, 3) (on pandas<3) currently return their own type, and with this PR they silently become ndarrays. The stubs also support this:
numpy/numpy/lib/_shape_base_impl.pyi
Lines 447 to 484 in a98529d
Closes numpy#17463. Ensure np.split, np.array_split, np.hsplit, np.vsplit, and np.dsplit accept sequence inputs without shape attribute (such as Python lists and tuples) by applying asanyarray only when shape is missing, preserving duck-typed arrays (e.g. JAX, pandas<3) that satisfy _SupportsSplitOps. Also fixes hsplit sequence handling and multi-dimensional sequence axis!=0 slice computation in array_split.
4227741 to
26ce9a5
Compare
|
Hi @jorenham, Thank you for catching that! That's a great point regarding duck-typed arrays satisfying I have updated the implementation so that
I also added The updated commit has been pushed to the PR. All 84 tests in |
|
Please don't use AI to speak for you; that goes against our AI policy; we prefer to talk to humans. |
PR summary
What problem does this PR solve?
Fixes np.split throws AttributeError for list/ tuple
aryand int sections, np.array_split does not #17463. Ensuresnp.split,np.array_split,np.hsplit,np.vsplit, andnp.dsplitconsistently acceptarray_likeinputs (such as Python lists and tuples) by applying_nx.asanyarray(ary)at entry and using_nx.ndim(ary) > 1inhsplit. This eliminates theAttributeError: 'list' object has no attribute 'shape'when splitting sequences with integer sections, and fixes incorrect slice calculations inarray_splitwhenaxis != 0.Why are you interested in working on this PR?
I noticed the inconsistency between passing a list of indices vs an integer section count to
np.split(e.g.np.split([1, 2, 3, 4], [2])worked, butnp.split([1, 2, 3, 4], 2)crashed withAttributeError), and saw that the type stubs in_shape_base_impl.pyialready expectedArrayLike.How does the proposed change help you?
It makes sequence splitting consistent across the entire family of split functions and prevents unexpected runtime
AttributeErrorcrashes for downstream code passing lists or tuples.First time contributor introduction
Hi! I'm Sai Krishna, a machine learning engineer and open source contributor working with NumPy and the scientific Python ecosystem. I use NumPy extensively for numerical computing and data processing pipelines.
AI Disclosure
I used an AI coding assistant (Google Antigravity) to help explore the codebase, draft the code modifications in
numpy/lib/_shape_base_impl.py, and draft test cases innumpy/lib/tests/test_shape_base.py. I personally reviewed the implementation details, confirmed thatasanyarraypreservesndarraysubclasses (such asMaskedArray) with zero-copy and is a no-op onndarray, and verified that all 83 tests intest_shape_base.pyand all 4,887 tests innumpy/lib/tests/pass cleanly without regression.