-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
DEP,BUG: Coercion/cast of array to a subarray dtype will be fixed #17596
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
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
DEP,BUG: Coercion/cast of array to a subarray dtype will be fixed
This currently appends the subarray dtype dimensions first and then tries to assign to the result array which uses incorrect broadcasting (broadcasting against the subarray dimensions instead of repeating each element according to the subarray dimensions). This also fixes the python scalar pathway `np.array(2, dtype="(2)f4,")` which previously only filled the first value. I consider that a clear bug fix. Closes gh-17511
- Loading branch information
commit fcc394033c5902bbd8104694c5f7d33a8b5eb99f
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,30 @@ | ||
Arrays cannot be using subarray dtypes | ||
-------------------------------------- | ||
Array creation and casting using ``np.array(arr, dtype)`` | ||
and ``arr.astype(dtype)`` will use different logic when ``dtype`` | ||
is a subarray dtype such as ``np.dtype("(2)i,")``. | ||
|
||
For such a ``dtype`` the following behaviour is true:: | ||
|
||
res = np.array(arr, dtype) | ||
|
||
res.dtype is not dtype | ||
res.dtype is dtype.base | ||
res.shape == arr.shape + dtype.shape | ||
|
||
But ``res`` is filled using the logic: | ||
|
||
res = np.empty(arr.shape + dtype.shape, dtype=dtype.base) | ||
res[...] = arr | ||
|
||
which uses incorrect broadcasting (and often leads to an error). | ||
In the future, this will instead cast each element individually, | ||
leading to the same result as:: | ||
|
||
res = np.array(arr, dtype=np.dtype(["f", dtype]))["f"] | ||
|
||
Which can normally be used to opt-in to the new behaviour. | ||
|
||
This change does not affect ``np.array(list, dtype="(2)i,")`` unless the | ||
``list`` itself includes at least one array. In particular, the behaviour | ||
is unchanged for a list of tuples. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -314,6 +314,24 @@ def test_union_struct(self): | |
'formats':['i1', 'O'], | ||
'offsets':[np.dtype('intp').itemsize, 0]}) | ||
|
||
@pytest.mark.parametrize(["obj", "dtype", "expected"], | ||
[([], ("(2)f4,"), np.empty((0, 2), dtype="f4")), | ||
(3, "(3)f4,", [3, 3, 3]), | ||
(np.float64(2), "(2)f4,", [2, 2]), | ||
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. To be clear, the first case here previously returned |
||
([((0, 1), (1, 2)), ((2,),)], '(2,2)f4', None), | ||
(["1", "2"], "(2)i,", None)]) | ||
def test_subarray_list(self, obj, dtype, expected): | ||
dtype = np.dtype(dtype) | ||
res = np.array(obj, dtype=dtype) | ||
|
||
if expected is None: | ||
# iterate the 1-d list to fill the array | ||
expected = np.empty(len(obj), dtype=dtype) | ||
for i in range(len(expected)): | ||
expected[i] = obj[i] | ||
|
||
assert_array_equal(res, expected) | ||
|
||
def test_comma_datetime(self): | ||
dt = np.dtype('M8[D],datetime64[Y],i8') | ||
assert_equal(dt, np.dtype([('f0', 'M8[D]'), | ||
|
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.