-
-
Notifications
You must be signed in to change notification settings - Fork 11.2k
ENH: Add ndmax parameter to np.array to control recursion depth #29569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d7509ec
ENH: add ndmax parameter to np.array
kibitzing 4f698ca
ENH: validate ndmax argument is positive
kibitzing fe974b9
TST: add ndmax tests for array creation
kibitzing ecaaee2
ENH: allow np.array with ndmax=0 to create 0-D array
kibitzing cfce2c2
MNT: simplify ndmax validation and error message
kibitzing dabaeda
MNT: improve consistency in error message formatting
kibitzing 0f1ca84
DOC: add comment explaining legacy behavior of max_depth=0
kibitzing 89f36ef
TST: update tests to reflect new ndmax validation and error message
kibitzing 9ae4f19
DOC: add documentation and examples for np.array ndmax parameter
kibitzing 8ba91fc
DOC: add release note for numpy.array ndmax parameter
kibitzing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
``ndmax`` option for `numpy.array` | ||
---------------------------------------------------- | ||
The ``ndmax`` option is now available for `numpy.array`. | ||
It explicitly limits the maximum number of dimensions created from nested sequences. | ||
|
||
This is particularly useful when creating arrays of list-like objects with ``dtype=object``. | ||
By default, NumPy recurses through all nesting levels to create the highest possible | ||
dimensional array, but this behavior may not be desired when the intent is to preserve | ||
nested structures as objects. The ``ndmax`` parameter provides explicit control over | ||
this recursion depth. | ||
|
||
.. code-block:: python | ||
|
||
# Default behavior: Creates a 2D array | ||
>>> a = np.array([[1, 2], [3, 4]], dtype=object) | ||
>>> a | ||
array([[1, 2], | ||
[3, 4]], dtype=object) | ||
>>> a.shape | ||
(2, 2) | ||
|
||
# With ndmax=1: Creates a 1D array | ||
>>> b = np.array([[1, 2], [3, 4]], dtype=object, ndmax=1) | ||
>>> b | ||
array([list([1, 2]), list([3, 4])], dtype=object) | ||
>>> b.shape | ||
(2,) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.