[Numpy-discussion] Nice float -> integer conversion?

Matthew Brett matthew.brett@gmail....
Tue Oct 11 13:06:21 CDT 2011


Have I missed a fast way of doing nice float to integer conversion?

By nice I mean, rounding to the nearest integer, converting NaN to 0,
inf, -inf to the max and min of the integer range?  The astype method
and cast functions don't do what I need here:

In [40]: np.array([1.6, np.nan, np.inf, -np.inf]).astype(np.int16)
Out[40]: array([1, 0, 0, 0], dtype=int16)

In [41]: np.cast[np.int16](np.array([1.6, np.nan, np.inf, -np.inf]))
Out[41]: array([1, 0, 0, 0], dtype=int16)

Have I missed something obvious?

See y'all,


More information about the NumPy-Discussion mailing list