API: Introduce np.astype [Array API]#25079
Conversation
numpy/_core/numeric.py
Outdated
| True | ||
|
|
||
| """ | ||
| x = asarray(x) |
There was a problem hiding this comment.
This isn't really right:
- You would have to use
copyin theasarraycall. - The
copy=copyinastype()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.
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.
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.
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.
| x : array_like | |
| x : `ndarray` |
Maybe also a note that array-likes are explicitly not supported here?
There was a problem hiding this comment.
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.
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.astypefor 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.astypearguments outside of the standard.Tracking issue: #25076