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

Tony Yu tsyu80@gmail....
Thu Jul 16 11:07:54 CDT 2009


Sorry, I don't know if its proper mailing-list-etiquette to bump my  
own post...

Are there any comments on whether this interp error is expected  
behavior?

Thanks,
-Tony

> Date: Mon, 13 Jul 2009 13:50:50 -0400
> From: Tony Yu <tsyu80@gmail.com>
> Subject: [Numpy-discussion] Using interpolate with zero-rank array
> 	raises	error
> To: Discussion of Numerical Python <numpy-discussion@scipy.org>
> Message-ID: <67526143-4424-4E06-BFE5-435091C78878@gmail.com>
> Content-Type: text/plain; charset="us-ascii"
>
> (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)
>
> Cheers,
> -Tony
>
> 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]
>    else:
>        return compiled_interp(x, xp, fp, left, right)
> =============================
>



More information about the NumPy-Discussion mailing list