8000 DEP: Deprecate coercion to subarray dtypes by seberg · Pull Request #17419 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

DEP: Deprecate coercion to subarray dtypes #17419

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 1 commit into from
Oct 7, 2020

Conversation

seberg
Copy link
Member
@seberg seberg commented Oct 1, 2020

When coercing to subarray dtypes, e.g. using np.array(obj, dtype),
but also arr.astype(dtype), the behaviour was only well defined
with tuple inputs, but not with array-like inputs.
In particular, arr.astype(dtype) had arguably surprising behaviour
of not converting by element, but rather attempting (and often failing)
to broadcast arr to the result array with added dimensions.

This deprecates all of these cases, the main issue would be for users
relying on stranger inputs with broadcasted tuples contained in
sequences:

np.array([((0, 1), (1, 2)), ((2,),)], dtype='(2,2)f4')

In most cases, where the tuples have the correct output shape,
the new base dtype can be directly used since the discovered shape
should match.
However, there is no work-around for the above case.

Closes gh-17173

When coercing to subarray dtypes, e.g. using `np.array(obj, dtype)`,
but also `arr.astype(dtype)`, the behaviour was only well defined
with tuple inputs, but not with array-like inputs.
In particular, `arr.astype(dtype)` had arguably surprising behaviour
of not converting by element, but rather attempting (and often failing)
to broadcast `arr` to the result array with added dimensions.

This deprecates all of these cases, the main issue would be for users
relying on stranger inputs with broadcasted tuples contained in
sequences:
```
np.array([((0, 1), (1, 2)), ((2,),)], dtype='(2,2)f4')
```

In most cases, where the tuples have the correct output shape,
the new base dtype can be directly used since the discovered shape
should match.
However, there is no work-around for the above case.

Closes numpygh-17173
@seberg seberg force-pushed the deprecate-coercion-to-subarray-dtype branch from 9c33587 to e31ae7f Compare October 1, 2020 20:53
@seberg seberg added the 62 - Python API Changes or additions to the Python API. Mailing list should usually be notified. label Oct 1, 2020
@seberg
Copy link
Member Author
seberg commented Oct 1, 2020

In a sense, the arguably correct solution would be to only do the "dimension absorbing" step after doing all the assignment (reshaping/viewing the array before returning). The problem is that this changes the behaviour of current array-likes the same way as in the arr.astype() case. But, if we want to go there, it may be possible to do a FutureWarning instead, and keep a nested list-of-tuples unchanged. The scalar case is another ugly wart:

In [2]: np.array(3, dtype="(5)i,")                                                                                      
Out[2]: array([ 3,  0,  0,  0, 32], dtype=int32)

since it returns uninitialized memory :(.

* WARNING: Do not use this function, it exists purely to support a deprecated
* code path.
*/
static int
Copy link
Member Author

Choose a reason for hiding this comment

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

In case it isn't obvious from the comment. Do not review this function, it is a straight copy from the 1.19.x branch.

@mattip mattip merged commit 3827583 into numpy:master Oct 7, 2020
@seberg seberg deleted the deprecate-coercion-to-subarray-dtype branch October 7, 2020 18:53
@mattip
Copy link
Member
mattip commented Oct 7, 2020

Putting this in. We may need to revert the deprecation if real use cases arise.

@mattip
Copy link
Member
mattip commented Oct 7, 2020

Thanks @seberg

@pllim
Copy link
Contributor
pllim commented Oct 8, 2020

Hello. astropy sees this deprecation warning while attempting to parse VO table. Any advise on how to mitigate this warning on our end? Please see astropy/astropy#10815 for more information. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
07 - Deprecation 62 - Python API Changes or additions to the Python API. Mailing list should usually be notified. component: numpy.dtype
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Regression in array creation np.array([...], dtype='(2,2)f4') RuntimeError (Zarr testsuite )
3 participants
0