[Numpy-discussion] confusing recarray behaviour

Jonathan Taylor jonathan.taylor at stanford.edu
Fri Apr 28 16:21:15 CDT 2006


I'm new to recarrays and have been struggling with them. I keep getting 
an exception

TypeError: expected a readable buffer object

with no informative traceback.

What I pass to N.array seems to agree with the examples in numpybook.

Below is an example that does work for me (excuse the longish example 
but it was just cut and paste to make my life easier). In my code, funny 
things happen
(see ipython excerpt below this). In particular, I have a list v with 
v[0:2] = V and with the
same dtype "ddesc" I get this exception when I change V to v[0:2].

Any help would be appreciated.

---------------------------------------------------------------------------------------
import numpy as N

timedesc = N.dtype({'names':['tm_year',     
                             'tm_mon',    
                             'tm_mday',
                             'tm_hour',
                             'tm_min',   
                             'tm_sec',
                             'tm_wday',
                             'tm_yday',
                             'tm_isdst'],
                    'formats':['i2']*9})

ddesc = N.dtype({'names': ('Week',
                          'Date',
                          'Institution',
                          'SeqNo',
                          'HeightDone',
                          'Height',
                          'UnitsH',
                          'WeightDone',
                          'Weight',
                          'Units',
                          'PulseDone',
                          'Pulse',
                          'BPdone',
                          'BPSys',
                          'BPDia',
                          'PID',
                          'RN'),
                'formats': ['f4',
                            timedesc] + ['f4']*15})
                                         


V = [(12.0, (2005, 4, 22, 0, 0, 0, 4, 112, -1), 501.0,
      1.0, 2.0, 0.0, 0, 1.0, 91.5, 1.0, 1.0, 87.0, 1.0, 129.0,
      76.0, 107.0, 11.0),
     (24.0, (2005, 2, 1, 0, 0, 0, 1, 32, -1), 504.0,
      1.0, 2.0, 0.0, 0, 1.0, 166.0, 2.0, 1.0, 84.0, 1.0, 128.0,
      78.0,  401.0, 7.0)
     ]

w=N.array(V, dtype=ddesc)

--------------------------------------------------------------------------------------------------

In [97]:v[0:2] == V
Out[97]:True

In [98]:N.array(V, ddesc)
Out[98]:
array([ (12.0, (2005, 4, 22, 0, 0, 0, 4, 112, -1), 501.0, 1.0, 2.0, 0.0, 0.0, 1.0, 91.5, 1.0, 1.0, 87.0, 1.0, 129.0, 76.0, 107.0, 11.0),
       (24.0, (2005, 2, 1, 0, 0, 0, 1, 32, -1), 504.0, 1.0, 2.0, 0.0, 0.0, 1.0, 166.0, 2.0, 1.0, 84.0, 1.0, 128.0, 78.0, 401.0, 7.0)],
      dtype=[('Week', '<f4'), ('Date', [('tm_year', '<i2'), ('tm_mon', '<i2'), ('tm_mday', '<i2'), ('tm_hour', '<i2'), ('tm_min', '<i2'), ('tm_sec', '<i2'), ('tm_wday', '<i2'), ('tm_yday', '<i2'), ('tm_isdst', '<i2')]), ('Institution', '<f4'), ('SeqNo', '<f4'), ('HeightDone', '<f4'), ('Height', '<f4'), ('UnitsH', '<f4'), ('WeightDone', '<f4'), ('Weight', '<f4'), ('Units', '<f4'), ('PulseDone', '<f4'), ('Pulse', '<f4'), ('BPdone', '<f4'), ('BPSys', '<f4'), ('BPDia', '<f4'), ('PID', '<f4'), ('RN', '<f4')])

In [99]:N.array(v[0:2], ddesc)
---------------------------------------------------------------------------
exceptions.TypeError                                 Traceback (most recent call last)

/home/jtaylo/svn/personal/projects/deescalate/python/<ipython console>

TypeError: expected a readable buffer object



-- 
------------------------------------------------------------------------
I'm part of the Team in Training: please support our efforts for the
Leukemia and Lymphoma Society!

http://www.active.com/donate/tntsvmb/tntsvmbJTaylor

GO TEAM !!!

------------------------------------------------------------------------
Jonathan Taylor                           Tel:   650.723.9230
Dept. of Statistics                       Fax:   650.725.8977
Sequoia Hall, 137                         www-stat.stanford.edu/~jtaylo
390 Serra Mall
Stanford, CA 94305

-------------- next part --------------
A non-text attachment was scrubbed...
Name: jonathan.taylor.vcf
Type: text/x-vcard
Size: 329 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/numpy-discussion/attachments/20060428/b707fc01/attachment.vcf 


More information about the Numpy-discussion mailing list