-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
API: Introduce np.astype
[Array API]
#25079
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
Conversation
numpy/_core/numeric.py
Outdated
True | ||
|
||
""" | ||
x = asarray(x) |
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.
This isn't really right:
- You would have to use
copy
in theasarray
call. - The
copy=copy
inastype()
doesn't have the same semantics.
I am actually not sure if astype
should coerce from to an array at all? If this does a forced cast, then astype
becomes asarray()
.
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.
I also wonder if any coercion should be allowed here and I would agree if it shouldn't.
Then e.g. np.astype([1,2,3], np.float64)
just wouldn't be supported.
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.
I think I agree about no coercion here. Instead, it seems to me like this should verify that the input is already an array.
That would leave the question of how exactly to do that. The dispatcher for __array_function__
was already added, so I think it should work to check with isinstance(x, np.ndarray)
(allows subclasses, but not duck arrays - those should be using __array_function__
).
And, if we do that, what's the right exception type? I'd think TypeError
?
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.
I second TypeError
. Its documentation says:
Raised when an operation or function is applied to an object of inappropriate type.
4b981b4
to
dc12606
Compare
dc12606
to
f986499
Compare
numpy/_core/numeric.py
Outdated
|
||
Parameters | ||
---------- | ||
x : array_like |
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.
x : array_like | |
x : `ndarray` |
Maybe also a note that array-likes are explicitly not supported here?
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.
Right, I updated it to x : ndarray
to follow existing docs in other functions.
I also added the note.
Co-authored-by: Bas van Beek <43369155+BvB93@users.noreply.github.com>
Thanks @mtsokol! |
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.
Uh, I noticed I misspelled PR number - fix is in #25054.
np.astype
[Array API]np.astype
[Array API]
Hi @rgommers @ngoldbaum,
This PR adds
np.astype
for Array API compatibility (https://data-apis.org/array-api/latest/API_specification/generated/array_api.astype.html).It adheres to Array API and uses default values for
ndarray.astype
arguments outside of the standard.Tracking issue: #25076