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
ENH: Allow creating structured void scalars by passing dtype #22316
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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,4 @@ | ||
``np.void`` now has a ``dtype`` argument | ||
---------------------------------------- | ||
NumPy now allows constructing structured void scalars directly by | ||
passing the ``dtype`` argument to ``np.void``. |
This file contains 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 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 |
---|---|---|
|
@@ -225,16 +225,52 @@ def add_newdoc_for_scalar_type(obj, fixed_aliases, doc): | |
|
||
add_newdoc_for_scalar_type('void', [], | ||
r""" | ||
Either an opaque sequence of bytes, or a structure. | ||
np.void(length_or_data, /, dtype=None) | ||
|
||
Create a new structured or unstructured void scalar. | ||
|
||
Parameters | ||
---------- | ||
length_or_data : int, array-like, bytes-like, object | ||
One of multiple meanings (see notes). The length or | ||
bytes data of an unstructured void. Or alternatively, | ||
the data to be stored in the new scalar when `dtype` | ||
is provided. | ||
This can be an array-like, in which case an array may | ||
be returned. | ||
dtype : dtype, optional | ||
If provided the dtype of the new scalar. This dtype must | ||
be "void" dtype (i.e. a structured or unstructured | ||
void). | ||
|
||
..versionadded:: 1.24 | ||
|
||
Notes | ||
----- | ||
For historical reasons and because void scalars can represent both | ||
arbitrary byte data and structured dtypes, the void constructor | ||
has three calling conventions: | ||
|
||
1. ``np.void(5)`` creates a ``dtype="V5"`` scalar filled with | ||
``\0`` bytes. The 5 can be a Python or NumPy integer. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How many |
||
2. ``np.void(b"bytes-like")`` creates a void scalar from | ||
the byte string. The dtype is chosen based on its length. | ||
seberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
3. When a ``dtype=`` is passed the call is rougly the same as an | ||
array creation. However a void scalar is returned when possible. | ||
seberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Please see the examples which show all three different conventions. | ||
|
||
Examples | ||
-------- | ||
>>> np.void(5) | ||
void(b'\x00\x00\x00\x00\x00') | ||
>>> np.void(b'abcd') | ||
void(b'\x61\x62\x63\x64') | ||
>>> np.void((5, 3.2, "eggs"), dtype="i,d,S5") | ||
(5, 3.2, b'eggs') # looks like a tuple, but is `np.void` | ||
>>> np.void(3, dtype=[('x', np.int8), ('y', np.int8)]) | ||
(3, 3) # looks like a tuple, but is `np.void` | ||
seberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Structured `void` scalars can only be constructed via extraction from :ref:`structured_arrays`: | ||
|
||
>>> arr = np.array((1, 2), dtype=[('x', np.int8), ('y', np.int8)]) | ||
>>> arr[()] | ||
(1, 2) # looks like a tuple, but is `np.void` | ||
""") | ||
|
||
add_newdoc_for_scalar_type('datetime64', [], | ||
|
This file contains 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 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 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this link to the definition of void https://numpy.org/devdocs/reference/arrays.scalars.html#numpy.void?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Linking to https://numpy.org/devdocs/user/basics.rec.html#structured-datatypes (lets see if that works), that seems useful. This is that documentation, so that would be circular ;).