[Numpy-discussion] numpy.genfromtxt converters issue
Lluís
xscript@gmx....
Sat Nov 6 10:52:47 CDT 2010
Damien Moore writes:
> 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.
> 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.]])
I think this is what you want:
>>> cat /tmp/test.csv
1, 2
3, 4
>>> numpy.genfromtxt("/tmp/test.csv", delimiter=",", dtype=float)
array([[ 1., 2.],
[ 3., 4.]])
Lluis
--
"And it's much the same thing with knowledge, for whenever you learn
something new, the whole world becomes that much richer."
-- The Princess of Pure Reason, as told by Norton Juster in The Phantom
Tollbooth
More information about the NumPy-Discussion
mailing list