FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

API: Introduce ``np.astype`` [Array API] by mtsokol · Pull Request #25079 · numpy/numpy · GitHub

/ numpy Public

API: Introduce np.astype [Array API] - #25079

Merged
ngoldbaum merged 1 commit into
numpy:mainfrom
mtsokol:numpy-astype
Dec 7, 2023
Merged

ngoldbaum merged 1 commit into
numpy:mainfrom
mtsokol:numpy-astype

Conversation

mtsokol commented Nov 6, 2023

Copy link
Copy Markdown
Member

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

Comment thread numpy/_core/numeric.py Outdated
True

"""
x = asarray(x)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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().

mtsokol Nov 7, 2023
edited
Loading

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I second TypeError. Its documentation says:

Raised when an operation or function is applied to an object of inappropriate type.

Comment thread numpy/_core/numeric.pyi
mtsokol force-pushed the numpy-astype branch 2 times, most recently from 4b981b4 to dc12606 Compare November 12, 2023 22:12
mtsokol added this to the 2.0.0 release milestone Nov 29, 2023
Comment thread numpy/_core/numeric.py Outdated

Parameters
----------
x : array_like

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
x : array_like
x : `ndarray`

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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>

Copy link
Copy Markdown
Member

Thanks @mtsokol!

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Uh, I noticed I misspelled PR number - fix is in #25054.

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
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
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


Back | FazBrowse Home | New Git URL