You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have already posted this as a stackoverflow question, but this seems the best place to post it.
I am incurring in a behaviour that I cannot comprehend, and it appears to me like a bug.
Problem description
I was hoping that trying to convert the data type of an array to integer would raise the "classic" ValueError: cannot convert float NaN to integer when the array contains NaN.
This unfortunately does not happen.
At a first glance this seems not happening because the NaN contained in a numpy array are converted to numpy.float64 instead of "remaining" float as per numpy.ndarray documentation.
In fact, consider this example:
importnumpyasnparr=np.array([np.nan, 1])
print(type(arr[0])) # Output: <class 'numpy.float64'># Converting numpy.float64 NaN to other data types## numpy integersprint( np.int8(arr[0])) # Output: 0 print(np.int32(arr[0])) # Output: 0print(np.int32(arr[0])) # Output: -2147483648 (i.e. -2 ** 31)print(np.int64(arr[0])) # Output: -9223372036854775808 (i.e. -2 ** 63)## numpy unsigned integersprint( np.uint8(arr[0])) # Output: 0print(np.uint16(arr[0])) # Output: 0print(np.uint32(arr[0])) # Output: 0print(np.uint64(arr[0])) # Output: 9223372036854775808## numpy floatsprint(np.float16(arr[0])) # Output: NaNprint(np.float32(arr[0])) # Output: NaNprint(np.float64(arr[0])) # Output: NaN# Converting numpy.float NaN to other data types## np.float and np.int are aliases to float and int respectively (both np.float and np.int are deprecated)type(np.nan) ==type(np.float(np.nan)) # Output: Trueprint(np.int(np.nan)) # Output: ValueError: cannot convert float NaN to integer## numpy integersprint( np.int8(np.nan)) # Output: ValueError: cannot convert float NaN to integerprint(np.int16(np.nan)) # Output: ValueError: cannot convert float NaN to integerprint(np.int32(np.nan)) # Output: ValueError: cannot convert float NaN to integerprint(np.int64(np.nan)) # Output: ValueError: cannot convert float NaN to integer## numpy unsigned integersprint( np.uint8(np.nan)) # Output: ValueError: cannot convert float NaN to integerprint(np.uint16(np.nan)) # Output: ValueError: cannot convert float NaN to integerprint(np.uint32(np.nan)) # Output: ValueError: cannot convert float NaN to integerprint(np.uint64(np.nan)) # Output: ValueError: cannot convert float NaN to integer## numpy floatsprint(np.float16(np.nan)) # Output: NaNprint(np.float32(np.nan)) # Output: NaNprint(np.float64(np.nan)) # Output: NaN
Questions
Why the elements of arr are converted to numpy.float64 instead of float in the first place?
In what exactly do python built-in float and numpy.float64 differ?
Why np.int32(arr) and np.int64(arr) contain the "smallest possible int" while np.int8(arr) and np.int16(arr) contain zeros? (Similar question for unsigned integer types)
NumPy/Python version information:
I am experiencing this behaviour on multiple platform/python/numpy versions, see the three examples below.
You can even get different results based on the length of the vector. Casting floats to integers is undefined in C if the integer value would be out of bound. I am working on floating FPEs given by the CPU/C for these casts, so at least most should give an "invalid value" warning at least. (For the Nan/Inf cases, for large out-of-bound values things get even more tricky it seems.)
I do not think anyone ever tried to actually raise the warning, it could be done, but I would not be surprised if it slows down the casts quite a bit.
All of these now give a warning on the main branch. Maybe we should escalate that, but that requires some more thought IMO, so I think we should start a new discussion about it.
The behavior can be modified using the np.errstate() mechanism.
Thus I will close the issue for now, but please don't hesitate to open a new one about guaranteeing an error being raised.
I have already posted this as a stackoverflow question, but this seems the best place to post it.
I am incurring in a behaviour that I cannot comprehend, and it appears to me like a bug.
Problem description
I was hoping that trying to convert the data type of an array to integer would raise the "classic"
ValueError: cannot convert float NaN to integer
when the array contains NaN.This unfortunately does not happen.
At a first glance this seems not happening because the NaN contained in a numpy array are converted to
numpy.float64
instead of "remaining"float
as pernumpy.ndarray
documentation.In fact, consider this example:
Questions
arr
are converted tonumpy.float64
instead offloat
in the first place?float
andnumpy.float64
differ?np.int32(arr)
andnp.int64(arr)
contain the "smallest possible int" whilenp.int8(arr)
andnp.int16(arr)
contain zeros? (Similar question for unsigned integer types)NumPy/Python version information:
I am experiencing this behaviour on multiple platform/python/numpy versions, see the three examples below.
WSL
Windows
Online W3Schools
The text was updated successfully, but these errors were encountered: