[Numpy-discussion] numpy.genfromtxt converters issue

Damien Moore damienlmoore@gmail....
Sat Nov 6 08:22:24 CDT 2010


Hi List,

I'm trying to import csv data as a numpy array using genfromtxt. The csv
file contains mixed data, some floating point, others string codes and dates
that I want to convert to floating point. The strange thing is that when I
use the 'converters' argument to convert a subset of the columns the
resulting output of genfromtxt becomes a 1d array of tuples instead of the
desired 2d array of floats. I've provided a simple example below. The output
I want should be numpy.array([[1,2],[3,4]]). Any thoughts on how to get my
desired output would be appreciated.

thanks,
Damien

import numpy, StringIO

s=StringIO.StringIO('q1,2\nq3,4')
a=numpy.genfromtxt(s,delimiter=',',converters={0:lambda s:float(s[1:])})
s=StringIO.StringIO('q1,2\nq3,4')
b=numpy.genfromtxt(s,delimiter=',')
a.shape
(2,)
b.shape
(2,2)
>>> a
array([(1.0, 2.0), (3.0, 4.0)],
      dtype=[('f0', '|O4'), ('f1', '<f8')])
>>> b
array([[ NaN,   2.],
       [ NaN,   4.]])
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20101106/7a0ded84/attachment.html 


More information about the NumPy-Discussion mailing list