Repository: numpy/numpy

Issue:
An error is given if a integer list is passed to nan_to_num (Trac #880)
_Original ticket http://projects.scipy.org/numpy/ticket/880 on 2008-08-06 by @bsouthey, assigned to unknown._

When an integer list is passed to nan_to_num then an error occurs. This does not occur if the list contains floats or a numpy integer array is used.

```
np.nan_to_num([1.0,3]) # returns: array([ 1.,  3.])
n=np.array([1,3])
np.nan_to_num(n) # returns:   array([1, 3])

np.nan_to_num([1,3])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.5/site-packages/numpy/lib/type_check.py", line 135, in nan_to_num
    maxf, minf = _getmaxmin(y.dtype.type)
  File "/usr/lib64/python2.5/site-packages/numpy/lib/type_check.py", line 103, in _getmaxmin
    f = getlimits.finfo(t)
  File "/usr/lib64/python2.5/site-packages/numpy/lib/getlimits.py", line 46, in __new__
    raise ValueError, "data type %r not inexact" % (dtype)
ValueError: data type <type 'numpy.int64'> not inexact
```



Discussion:
Milestone changed to `1.4.0` by @cournape on 2009-03-09

Milestone changed to `Unscheduled` by @mwiebe on 2011-03-23

Still present in 1.9-devel.


Resolving patch:
diff --git a/numpy/lib/type_check.py b/numpy/lib/type_check.py
--- a/numpy/lib/type_check.py
+++ b/numpy/lib/type_check.py
@@ -324,12 +324,13 @@ def nan_to_num(x):
 
     Returns
     -------
-    out : ndarray, float
-        Array with the same shape as `x` and dtype of the element in `x`  with
-        the greatest precision. NaN is replaced by zero, and infinity
-        (-infinity) is replaced by the largest (smallest or most negative)
-        floating point value that fits in the output dtype. All finite numbers
-        are upcast to the output dtype (default float64).
+    out : ndarray
+        New Array with the same shape as `x` and dtype of the element in
+        `x`  with the greatest precision. If `x` is inexact, then NaN is
+        replaced by zero, and infinity (-infinity) is replaced by the
+        largest (smallest or most negative) floating point value that fits
+        in the output dtype. If `x` is not inexact, then a copy of `x` is
+        returned.
 
     See Also
     --------
@@ -354,33 +355,22 @@ def nan_to_num(x):
             -1.28000000e+002,   1.28000000e+002])
 
     """
-    try:
-        t = x.dtype.type
-    except AttributeError:
-        t = obj2sctype(type(x))
-    if issubclass(t, _nx.complexfloating):
-        return nan_to_num(x.real) + 1j * nan_to_num(x.imag)
-    else:
-        try:
-            y = x.copy()
-        except AttributeError:
-            y = array(x)
-    if not issubclass(t, _nx.integer):
-        if not y.shape:
-            y = array([x])
-            scalar = True
-        else:
-            scalar = False
-        are_inf = isposinf(y)
-        are_neg_inf = isneginf(y)
-        are_nan = isnan(y)
-        maxf, minf = _getmaxmin(y.dtype.type)
-        y[are_nan] = 0
-        y[are_inf] = maxf
-        y[are_neg_inf] = minf
-        if scalar:
-            y = y[0]
-    return y
+    x = _nx.array(x, subok=True)
+    xtype = x.dtype.type
+    if not issubclass(xtype, _nx.inexact):
+        return x
+
+    iscomplex = issubclass(xtype, _nx.complexfloating)
+    isscalar = (x.ndim == 0)
+
+    x = x[None] if isscalar else x
+    dest = (x.real, x.imag) if iscomplex else (x,)
+    maxf, minf = _getmaxmin(x.real.dtype)
+    for d in dest:
+        _nx.copyto(d, 0.0, where=isnan(d))
+        _nx.copyto(d, maxf, where=isposinf(d))
+        _nx.copyto(d, minf, where=isneginf(d))
+    return x[0] if isscalar else x
 
 #-----------------------------------------------------------------------------
 


Test patch:
diff --git a/numpy/lib/tests/test_type_check.py b/numpy/lib/tests/test_type_check.py
--- a/numpy/lib/tests/test_type_check.py
+++ b/numpy/lib/tests/test_type_check.py
@@ -277,6 +277,8 @@ def test_generic(self):
     def test_integer(self):
         vals = nan_to_num(1)
         assert_all(vals == 1)
+        vals = nan_to_num([1])
+        assert_array_equal(vals, np.array([1], np.int))
 
     def test_complex_good(self):
         vals = nan_to_num(1+1j)