[Numpy-discussion] different behaviour in asfarray(None)
Charles R Harris
charlesr.harris@gmail....
Tue Apr 22 19:18:51 CDT 2008
On Tue, Apr 22, 2008 at 5:51 PM, Charles R Harris <charlesr.harris@gmail.com>
wrote:
>
>
> On Tue, Apr 22, 2008 at 4:00 PM, Travis E. Oliphant <
> oliphant@enthought.com> wrote:
>
> > Stéfan van der Walt wrote:
> > > 2008/4/22 lorenzo bolla <lbolla@gmail.com>:
> > >
> > >> I noticed a change in the behaviour of numpy.asfarray between numpy
> > version
> > >> 1.0.5 and 1.1.0:
> > >>
> > >> 1.0.5
> > >> ====
> > >>
> > >> In [3]: numpy.asfarray(None)
> > >> Out[3]: array(nan)
> > >> In [4]: numpy.__version__
> > >> Out[4]: '1.0.5.dev4455'
> > >>
> > >> 1.1.0
> > >> ====
> > >>
> > >> In [16]: numpy.asfarray(None)
> > >>
> > ---------------------------------------------------------------------------
> > >> <type 'exceptions.TypeError'>: float() argument must be a string or a
> > number
> > >>
> > >> Is this intended? why?
> > >>
> > >
> > > Yes, 'asfarray' is equivalent to
> > >
> > > array(input).astype(dtype)
> > >
> > > I think it would be wrong to assume that None means NaN.
> > >
> > I'm curious who made the change and why. There was code intentionally
> > there to interpret None as nan for float arrays. So, I don't
> > understand why and/or when it changed.
> >
>
> Could be here:
>
> static double
> MyPyFloat_AsDouble(PyObject *obj)
> {
> double ret = 0;
> PyObject *num = PyNumber_Float(obj);
>
> if (num == NULL) {
> return _getNAN();
> }
> ret = PyFloat_AsDouble(num);
> Py_DECREF(num);
> return ret;
> }
>
> I don't know what PyNumber_Float does with None. I suspect all we need to
> do is call PyErr_Clear() in the if. The change was made so floats could be
> passed as strings.
>
And that does fix that problem. However, the fix need to be a bit more
complicated because evidently the array routine depends on an error return
to find strings, i.e, the simple approach of removing the error gives
float32('1.2')
as
array([1, nan, 2], dtype=float32), which is almost as annoying as
In [4]: int8('123')
Out[4]: array([1, 2, 3], dtype=int8)
which really needs to be fixed also. Can we please check for strings
*before* calling the conversion routines?
Chuck
>
> > -Travis
> >
> > _______________________________________________
> > Numpy-discussion mailing list
> > Numpy-discussion@scipy.org
> > http://projects.scipy.org/mailman/listinfo/numpy-discussion
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080422/52a78061/attachment.html
More information about the Numpy-discussion
mailing list