Repository navigation
BUG: Reset an unrepresentable fill_value on a dtype change - #32508
Conversation
jdavies-st
left a comment
There was a problem hiding this comment.
Given how insidious the original bug was, this is a needed fix. And the fix is at the right place.
The other option would be to fix the _check_fill_value() function itself, to raise a FloatingPointError instead of only a warning. But the impact radius of this might be higher, so this is probably a sufficient fix here, as-is.
I think even if we somehow in the future can separate out user fill value and default fill value, i.e. separate out intended fill value vs inadvertent fill value (as the bug this fixes was), then this fix is still necessary to get meaningful fill values where there's a floating point overflow. So valuable regardless.
697f5ac to
4e5dfa2
Compare
|
Thanks, I agree this is correct and in the vein of what we ended up with for #32423. If you want to follow up, there is still a remaining code path that needs a similar fix: This happens because |
|
Also argh unfortunately I missed it before merging but if you have time or interest it would be nice to make this and #32423 share a release note. You can make the content of the release note identical for both PRs and then towncrier will generate a changelog entry that links to both PRs. |
|
Thanks for pointing out the remaining path. I'll open a follow-up that moves the existing fallback into a shared helper used by both |
Co-authored-by: Yeonho Kim <168209377+carrot-dev01@users.noreply.github.com>
The backport of #32508 brought with it a number of new tests, this fixes a test failure by adding a needed import.
BUG: Reset an unrepresentable fill_value on a dtype change (#32508)
PR summary
As discussed in gh-28255. Since #32423,
MaskedArray._update_fromvalidates an inheritedfill_valueagainst the new dtype and falls back to the default when the cast fails. It decides "fails" by catching an exception, which misses casts that fail through the floating point error state instead.1e20is not a value the user picked, it is the default fill_value for float64, and reading the attribute stores it as a 0-d array. Casting that storednp.float64(1e20)to int64 does not raise: it sets theinvalidflag, warns, and returns an out-of-range integer (INT64_MIN on x86; INT64_MAX on the macOS machine in the issue thread). A Python float1e20passed directly goes through the scalar path and raises, which is whyfill_value=1e20at construction already fails cleanly.This wraps that call in
np.errstate(invalid='raise')and addsFloatingPointErrorto the except clause, so the fallback added in #32423 also runs for this case.Scope:
invalidis raised.overis left alone on purpose: a float fill_value that saturates toinfin a narrower float dtype is still a usable fill_value, and replacing it with the default would not be an improvement.invalidflag is platform dependent.-1.0touint8sets no flag on x86 and wraps to 255, so this change does not touch it there; on platforms where it does set the flag (BUG: MaskedArray.astype('uint8') with certain fill_value raises warning on ARM (Mac M3) inside Docker (Ubuntu 24.04/25.04) and leads inconsistent output #28403), the fill_value now falls back to the default instead of a platform-specific value.arr.fill_value = ...still raises, since the setter calls_check_fill_valuedirectly.fill_valuestoring the default, separating default and user fill values) are not addressed here.Tests: the new test compares against the result when
fill_valuewas never read rather than against a specific number, so it does not depend on the platform or on the default integer width. In gh-28255 I said this would not touch existing tests; running the fullnumpy/masuite showed thatTestMaskedArrayFunctions.test_whereinnumpy/ma/tests/test_core.pyasserted theRuntimeWarningfrom exactly this cast (set_fill_value(1e20)followed byastype(int)). It now asserts that the cast emits no warning and thatixm.fill_valueis the default for the new dtype. The rest ofnumpy/mapasses unchanged (run against 2.5.2 with the change applied).Fixes #28255
First time committer introduction
Not my first PR (#32405 was a whitespace fix), but my first change to
numpy.ma. I came tonumpy.mathrough gh-15601, which I am also preparing a fix for, and found this one while going through openmaissues around the time #32423 was being reviewed.AI Disclosure
I used Claude throughout: to help investigate the cause, to draft the two-line change, the tests, and the release note from my notes, and to put this description and the code comments into English. I reviewed and applied the changes, and ran the reproducer, the new tests, and the
numpy/masuite myself.