[Numpy-discussion] Suggested change for NaN, Infs float->int conversion

Matthew Brett matthew.brett at gmail.com
Sun Jan 25 04:35:25 EST 2009


Hi,

When converting arrays from float to ints, I notice that NaNs, Infs,
and -Infs all get the minimum integer value:

>>> flts = np.array([np.nan, np.inf, -np.inf])
>>> flts.astype(np.int16)
array([-32768, -32768, -32768], dtype=int16)

However, setting NaNs into integer arrays gives a value of 0

>>> ints = np.array([1])
>>> ints.dtype
dtype('int32')
>>> ints[0] = np.nan
>>> ints
array([0])

whereas Inf or -Inf raise an error (as Josef pointed out recently):

>> ints[0] = np.inf
------------------------------------------------------------
Traceback (most recent call last):
  File "<ipython console>", line 1, in <module>
OverflowError: cannot convert float infinity to long

>>> ints[0] = -np.inf
------------------------------------------------------------
Traceback (most recent call last):
  File "<ipython console>", line 1, in <module>
OverflowError: cannot convert float infinity to long

Matlab seems more consistent and sensible here:

>> flts = [NaN Inf -Inf];
>> int32(flts)

ans =

           0  2147483647 -2147483648

>> ints = int32([1 1 1]);
>> ints(:) = [NaN Inf -Inf]

ints =

           0  2147483647 -2147483648

Is there a route to change towards the matlab behavior?  Or at least
make numpy behavior self-consistent?

Best,

Matthew



More information about the NumPy-Discussion mailing list