[Numpy-discussion] OverflowError using _nanop with unsigned integers

Tony S Yu tsyu80 at gmail.com
Thu Nov 19 15:30:44 EST 2009


Functions that call _nanop (i.e. nan[arg]min, nan[arg]max) currently fail with unsigned integers. For example:

>>> np.nanmin(np.array([0, 1], dtype=np.uint8))

OverflowError: cannot convert float infinity to integer

It seems that unsigned integers don't get identified as integers in the _nanop function. Checking against `np.integer` instead of the built-in `int` will catch signed and unsigned integers, as shown below.


Note: the diff below also contains a change I made to fix ticket #1177

Index: numpy/lib/function_base.py
--- numpy/lib/function_base.py	(revision 7749)
+++ numpy/lib/function_base.py	(working copy)
@@ -1038,6 +1038,8 @@
     if isinstance(x, (float, int, number)):
         return compiled_interp([x], xp, fp, left, right).item()
+    elif isinstance(x, np.ndarray) and x.ndim == 0:
+        return compiled_interp(x[np.newaxis], xp, fp, left, right)[0]
         return compiled_interp(x, xp, fp, left, right)
@@ -1349,7 +1351,7 @@
     mask = isnan(a)
     # We only need to take care of NaN's in floating point arrays
-    if not np.issubdtype(y.dtype, int):
+    if not np.issubdtype(y.dtype, np.integer):
        y[mask] = fill
     res = op(y, axis=axis)

More information about the NumPy-Discussion mailing list