[Numpy-discussion] Issue with converting from numpy record to list/tuple

Francesc Altet faltet at carabos.com
Tue Aug 28 06:14:22 EDT 2007


A Monday 27 August 2007, Sean Davis escrigué:
> On 8/27/07, Sean Davis <seandavi at gmail.com> wrote:
> > I have a numpy recarray that I want to load into a database using
> > insert statements.  To do so, I need to convert each record to a
> > tuple.  Here is what I get (using psycopg2)
> >
> > In [1]: a[1]
> > Out[1]: ('5151_0023_0001', 'FORWARD', 'interval rank', 'target_tm:
> > 76.00 ;probe_tm:70.90;freq:27.93;count:01;rules:0000;score:0658',
> > 'chr3:1-199501827',
> > 'AAAGGAATTCCATTCATCTCTGGATATTTTGAAATCATTAGGGCAAACAATAAATAA', 0L,
> > 171449529L, 171449529L, 1L, 23L, 'experimental', 'CHR03P006149104',
> > 6149104L, 5151L, 23L, 1L)
> >
> > In [2]: type(a[1])
> > Out[2]: <class 'numpy.core.records.record'>
> >
> > In [3]: sqlcommand
> > Out[3]: 'insert into nbl_tmp values
> > (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);'
> >
> > In [4]: cur.execute(sqlcommand,tuple(a[1]))
> >
> > -------------------------------------------------------------------
> >-------- <class 'psycopg2.ProgrammingError'>       Traceback (most
> > recent call last)
> >
> > /sherlock/sdavis/Documents/workspace/svn/watson/Sean/PythonCode/<ip
> >ython console> in <module>()
> >
> > <class 'psycopg2.ProgrammingError'>: can't adapt
> >
> > In [5]: b=('5151_0023_0001', 'FORWARD', 'interval rank',
> > 'target_tm: 76.00
> > ;probe_tm:70.90;freq:27.93;count:01;rules:0000;score:0658',
> > 'chr3:1-199501827',
> > 'AAAGGAATTCCATTCATCTCTGGATATTTTGAAATCATTAGGGCAAACAATAAATAA', 0L,
> > 171449529L, 171449529L, 1L, 23L, 'experimental', 'CHR03P006149104',
> > 6149104L, 5151L, 23L, 1L)
> >
> > In [6]: cur.execute(sqlcommand,b)
> >
> > In [7]: a[1].dtype
> > Out[7]: dtype([('PROBE_DESIGN_ID', '|S40'), ('CONTAINER', '|S40'),
> > ('DESIGN_NOTE', '|S80'), ('SELECTION_CRITERIA', '|S80'), ('SEQ_ID',
> > '|S40'), ('PROBE_SEQUENCE', '|S100'), ('MISMATCH', '<u8'),
> > ('MATCH_INDEX', '<u8'), ('FEATURE_ID', '<u8'), ('ROW_NUM', '<u8'),
> > ('COL_NUM', '<u8'), ('PROBE_CLASS', '|S40'), ('PROBE_ID', '|S40'),
> > ('POSITION', '<u8'), ('DESIGN_ID', '<u8'), ('X', '<u8'), ('Y',
> > '<u8')])
> >
> > Why does the casting using tuple() not work while cut-and-paste of
> > the a[1] record into a new variable works just fine?
>
> I answered part of the question myself.  In the coercion back to
> tuple from a record, the datatypes remain numpy datatypes.  Is there
> a way to convert back from numpy datatypes to standard python types
> (string, int, float, etc.) without needing to check every numpy type
> and determine the appropriate python type?  In other words, is there
> a single function that I can feed a numpy type to (or a variable that
> has a numpy type) and have the standard python type (or an
> appropriately-coerced variable)?

Use .tolist() method.  Here is an example:

In [92]: r=numpy.empty(5, 'f8,i4,f8')

In [93]: type(tuple(r[0])[0])
Out[93]: <type 'numpy.float64'>

In [94]: type(r[0].tolist()[0])
Out[94]: <type 'float'>

HTH,

-- 
>0,0<   Francesc Altet     http://www.carabos.com/
V   V   Cárabos Coop. V.   Enjoy Data
 "-"



More information about the NumPy-Discussion mailing list