On 11.10.2011, at 9:18PM, josef.p...@gmail.com wrote: >> >> In [42]: c = np.zeros(4, np.int16) >> In [43]: d = np.zeros(4, np.int32) >> In [44]: np.around([1.6,np.nan,np.inf,-np.inf], out=c) >> Out[44]: array([2, 0, 0, 0], dtype=int16) >> >> In [45]: np.around([1.6,np.nan,np.inf,-np.inf], out=d) >> Out[45]: array([ 2, -2147483648, -2147483648, -2147483648], >> dtype=int32) >> >> Perhaps a starting point to harmonise this behaviour and get it closer to >> your expectations (it still would not be really nice having to define the >> output array first, I guess)... > > what numpy is this? > This was 1.6.1 I did suppress a RuntimeWarning that was raised on the first call, though: In [33]: np.around([1.67,np.nan,np.inf,-np.inf], decimals=1, out=d) /sw/lib/python2.7/site-packages/numpy/core/fromnumeric.py:37: RuntimeWarning: invalid value encountered in multiply result = getattr(asarray(obj),method)(*args, **kwds)
>>>> np.array([1.6, np.nan, np.inf, -np.inf]).astype(np.int16) > array([ 1, -32768, -32768, -32768], dtype=int16) >>>> np.__version__ > '1.5.1' >>>> a = np.ones(4, np.int16) >>>> a[:]=np.array([1.6, np.nan, np.inf, -np.inf]) >>>> a > array([ 1, -32768, -32768, -32768], dtype=int16) > > > I thought we get ValueError to avoid nan to zero bugs > >>>> a[2] = np.nan > Traceback (most recent call last): > File "<pyshell#22>", line 1, in <module> > a[2] = np.nan > ValueError: cannot convert float NaN to integer On master, an integer out raises a TypeError for any float input - not sure I'd consider that an improvement… >>> np.__version__ '2.0.0.dev-8f689df' >>> np.around([1.6,-23.42, -13.98, 0.14], out=c) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/derek/lib/python2.7/site-packages/numpy/core/fromnumeric.py", line 2277, in around return _wrapit(a, 'round', decimals, out) File "/Users/derek/lib/python2.7/site-packages/numpy/core/fromnumeric.py", line 37, in _wrapit result = getattr(asarray(obj),method)(*args, **kwds) TypeError: ufunc 'rint' output (typecode 'd') could not be coerced to provided output parameter (typecode 'h') according to the casting rule “same_kind“ I thought the NaN might have been dealt with first, before casting to int, but that doesn't seem to be the case (on master, again): >>> np.around([1.6,np.nan,np.inf,-np.inf]) array([ 2., nan, inf, -inf]) >>> np.around([1.6,np.nan,np.inf,-np.inf]).astype(np.int16) array([2, 0, 0, 0], dtype=int16) >>> np.around([1.6,np.nan,np.inf,-np.inf]).astype(np.int32) array([ 2, -2147483648, -2147483648, -2147483648], dtype=int32) Cheers, Derek _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion