-
-
Notifications
You must be signed in to change notification settings - Fork 11.2k
ENH: add a casting option 'same_value' and use it in np.astype #29129
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
Open
mattip
wants to merge
28
commits into
numpy:main
Choose a base branch
from
mattip:value-based
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+564
−145
Open
Changes from 20 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
cb8ea97
start implementing same_value casting
mattip 3859a73
work through more places that check 'cast', add a TODO
9dc63a5
add a test, percolate casting closer to inner loops
mattip 7cad6af
use SAME_VALUE_CAST flag for one inner loop variant
mattip 03e9ceb
aligned test of same_value passes. Need more tests
mattip 93b8bce
handle unaligned casting with 'same_value'
mattip 87e2487
extend tests to use source-is-complex
mattip 9aa1e5a
fix more interfaces to pass casting around, disallow using 'same_valu…
mattip 953714e
raise in places that have a kwarg casting, besides np.astype
mattip cd3e144
refactor based on review comments
mattip 6d6b045
CHAR_MAX,MIN -> SCHAR_MAX,MIN
mattip 1293657
copy context flags
mattip d151c91
add 'same_value' to typing stubs
mattip 6254ae5
document new feature
mattip 6922b35
test, check exact float->int casting: refactor same_value check into …
mattip a323a4b
enable astype same_value casting for scalars
mattip aec8ea1
typo
mattip 26c0fa1
fix ptr-to-src_value -> value casting errors
mattip 0846081
fix linting and docs, ignore warning better
mattip 76e01c1
gcc warning is different
mattip 963ea05
fixes from review, typos
mattip 4a9a498
fix compile warning ignore and make filter in tests more specific, di…
mattip 10c4493
fix warning filters
mattip 58a0a09
emit PyErr inside the loop
mattip 64b8747
macOS can emit FPEs when touching NAN
mattip 9d55847
Fix can-cast logic everywhere for same-value casts (only allow numeric)
seberg a800154
reorder and simplify, from review
mattip 70d92bc
revert last commit and remove redundant checks
mattip File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
``'same_value'`` for casting by value | ||
------------------------------------- | ||
The ``casting`` kwarg now has a ``'same_value'`` option that checks the actual | ||
values can be round-trip cast without changing value. Currently it is only | ||
implemented in `ndarray.astype`. This will raise a ``ValueError`` if any of the | ||
values in the array would change as a result of the cast, including rounding of | ||
floats or overflowing of ints. |
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
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -227,6 +227,8 @@ typedef enum { | |
NPY_SAME_KIND_CASTING=3, | ||
/* Allow any casts */ | ||
NPY_UNSAFE_CASTING=4, | ||
/* Allow any casts, check that no values overflow/change */ | ||
NPY_SAME_VALUE_CASTING=5, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that it really works exactly for builtin numeric types only, do you care for making it public on the C-API? I still expect the only reasonable way to make this public will be to have the two states, which somewhat makes me want to give this a value of |
||
} NPY_CASTING; | ||
|
||
typedef enum { | ||
|
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If we make this public on the C-API, I think we should be clear that this is only supported by cast safety requests and not to define a cast safety.
Should also add
.. versionadded:: ...
with a note that on previous numpy version it will behave the same as "unsafe".(or you guard the define with an
NPY_FEATURE_VERSION
or both. I would be fine with just a note that it is effectively ignored on older versions also.)