[Numpy-discussion] numpy.genfromtxt converters issue

josef.pktd@gmai... josef.pktd@gmai...
Sat Nov 6 11:45:48 CDT 2010


On Sat, Nov 6, 2010 at 11:52 AM, Damien Moore <damienlmoore@gmail.com> wrote:
> In reply to my own question, the trivial, but massively inefficient solution
> is:
>
> s=StringIO.StringIO('q1,2\nq3,4')
> a=numpy.genfromtxt(s,delimiter=',',converters={0:lambda s:float(s[1:])})
> a1 = numpy.array(a.tolist())
>
> But what I really want to do is have genfromtxt do the conversion for me.
> Specifically, if I specify a dtype like:
>
> a=numpy.genfromtxt(s,delimiter=',',dtype=float,converters={0:lambda
> s:float(s[1:])})
>
> then, ideally from my perspective, genfromtxt should coerce values to the
> dtype choice (after the converter has done its work) and output a 2d array
> or report an error. Instead it seems like whenever a converter is used,
> dtype gets ignored and a 1d array of tuples is always returned.
>
> More generally, it seems like the caller should have more control over what
> genfromtxt returns, whether a 1d array of tuples or the 2d array of a
> specific type.

It seems that genfromtxt doesn't check the return type from the
converter in this case.

This is how I get to what you want with some detours

>>> s=StringIO.StringIO('q1,2\nq3,4')
>>> a=numpy.genfromtxt(s,delimiter=',',converters={0:lambda s:float(s[1:])},dtype=[float, float])
>>> a
array([(1.0, 2.0), (3.0, 4.0)],
      dtype=[('f0', '<f8'), ('f1', '<f8')])
>>> a.view(float)
array([ 1.,  2.,  3.,  4.])
>>> a.view(float).reshape(a.shape[0], -1)
array([[ 1.,  2.],
       [ 3.,  4.]])

Josef

>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>


More information about the NumPy-Discussion mailing list