[Numpy-discussion] Using interpolate with zero-rank array raises error

Tony Yu tsyu80@gmail....
Mon Jul 13 12:50:50 CDT 2009

(Sorry if this is a duplicate; I think sent this from the wrong email  
the first time)

When using interpolate with a zero-rank array, I get "ValueError:  
object of too small depth for desired array". The following code  
reproduces this issue

 >>> import numpy as np
 >>> x0 = np.array(0.1)
 >>> x = np.linspace(0, 1)
 >>> y = np.linspace(0, 1)
 >>> np.interp(x0, x, y)

I hesitate to call this behavior a bug (when I've done this in the  
past, I find out I'm just doing something wrong), but I find the error  
unnecessary (and the error output a bit obscure).

Below is a (poorly tested) fix, which seems to work for me. (Sorry I'm  
not on svn, so it isn't a proper diff)


Python 2.5.1
numpy 1.3.0

numpy/lib/function_base.py: line 1144
    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)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090713/7d2de5cd/attachment.html 

More information about the NumPy-Discussion mailing list