Skip to content

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

Merged
merged 1 commit into from
Dec 7, 2023
Merged

Conversation

mtsokol
Copy link
Member

@mtsokol mtsokol commented Nov 6, 2023

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

True

"""
x = asarray(x)
Copy link
Member

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 the asarray call.
  • The copy=copy in astype() 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().

Copy link
Member Author

@mtsokol mtsokol Nov 7, 2023

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.

Copy link
Member

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?

Copy link
Member Author

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.

@mtsokol mtsokol force-pushed the numpy-astype branch 2 times, most recently from 4b981b4 to dc12606 Compare November 12, 2023 22:12
@mtsokol mtsokol added this to the 2.0.0 release milestone Nov 29, 2023

Parameters
----------
x : array_like
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
x : array_like
x : `ndarray`

Maybe also a note that array-likes are explicitly not supported here?

Copy link
Member Author

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>
@ngoldbaum
Copy link
Member

Thanks @mtsokol!

@ngoldbaum ngoldbaum merged commit 81f7200 into numpy:main Dec 7, 2023
@mtsokol mtsokol deleted the numpy-astype branch December 7, 2023 22:13
Copy link
Member Author

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.

@charris charris changed the title API: Introduce np.astype [Array API] API: Introduce np.astype [Array API] Dec 8, 2023
BvB93 added a commit to BvB93/numpy that referenced this pull request Dec 21, 2023
BvB93 added a commit to BvB93/numpy that referenced this pull request Dec 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants