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
BUG: Conversion of numpy.nan to int gives inconsistent results #21166
Comments
Hello, Thank you for reporting this issue. I have the same problem ( The the Numpy main casting function defined here does not check special floating point numbers like NaN, -Inf, +Inf (nor out-of-bounds values). This is due to the basic double-to-int cast performed in this same function as is an undefined behavior in C. For more information about this behavior in the C standard, please read this. This problem is closely related to the undefined behavior found out in #21123 . While fixing this is relatively straightforward, the actual question is: what is the expected behavior in this case in Numpy? |
Personally, I would expect an error to be thrown. It does not really make sens to convert an entity to an integer if the entity is "not a number". |
Indeed, NumPy is bad about producing floating point warnings for this kind of casts – it basically never even tried. Note that CPUs/compilers should not be so lazy: This should be a NumPy issue – although I would not be surprised if some compilers misbehave. I.e. it should be fairly straight forward to ensure that a warning is reliably given here. About the general undefined behaviour: It is observed fairly regularly. It would seem great to define a "correct" result, like 0 or the minimum value. But probably only if it has no major impact on speed. Unfortunately, that seems unlikely. I am optimistically marking this as a "project", but only for the part about checking floating point warnings for casts. (There should be duplicate issues open, so we may end up consolidating and close this one though.) EDIT: If anyone wants to dive into this. We need to copy some of the logic that |
I am getting the same error. |
I'm interested in working on this issue, I'll start looking into it right now |
Removing the "Project" label. I started working on this (I really need at least the part that |
The results also seem to depend on the target type, see this question on Stack Overflow, which is probably also related: npnan = np.float64(np.nan)
print(npnan.astype('int8')) # Output: 0
print(npnan.astype('int16')) # Output: 0
print(npnan.astype('int32')) # Output: -2147483648 (i.e. -2 ** 31)
print(npnan.astype('int64')) # Output: -9223372036854775808 (i.e. -2 ** 63) (These are from numpy 1.21.6 or 1.22.3 on debian.) Edit: see #21364 |
Going to close this. This now will give:
Which will honor the xref gh-21437 |
Describe the issue:
When converting a numpy array containing
numpy.nan
to typeint
, thenumpy.nan
are replaced by either-9223372036854775808
or0
depending on the computer.numpy.nan
is replaced by-9223372036854775808
on a Mac Pro (2019).numpy.nan
is replaced by0
on a MacBook Air (M1, 2020).Computers have the same version of macOS (12.2.1), Python (3.9.10) and numpy (1.22.2).
Expected output
Both computers should behave the same. Either throw an error, or return the same value.
Reproduce the code example:
Error message:
No response
NumPy/Python version information:
1.22.2 3.9.10 (main, Jan 15 2022, 11:40:53)
[Clang 13.0.0 (clang-1300.0.29.3)]
The text was updated successfully, but these errors were encountered: