[Numpy-discussion] mrecarray

Michael Droettboom mdroe@stsci....
Mon Jul 14 12:53:09 CDT 2008

I'm running into a couple of small problems with mrecarray.  I'm not 
sure if they're bugs or a usage error.

First, the constructor throws an exception when the format string 
contains nested arrays (if that is the proper term) such as "(2,2)f8".  
This creates a three-element tuple in the dtype.descr list, whereas the 
mrecord.py code seems to assume each descr will always have 2 elements. 


In [1]: from numpy.ma import mrecords

In [2]: x = mrecords.mrecarray(1, formats="(2,2)f8")
<type 'exceptions.ValueError'> Traceback (most recent call last)

/wonkabar/data1/scraps/<ipython console> in <module>()

/home/mdroe/usr/lib/python2.5/site-packages/numpy/ma/mrecords.py in 
__new__(cls, shape, dtype, buf, offset, strides, formats, names, titles, 
byteorder, aligned, mask, hard_mask, fill_value, keep_mask, copy, **options)
121 self = recarray.__new__(cls, shape, dtype=dtype, buf=buf, offset=offset,
122 strides=strides, formats=formats,
--> 123 byteorder=byteorder, aligned=aligned,)
124 #
125 mdtype = [(k,'|b1') for (k,_) in self.dtype.descr]

/home/mdroe/usr/lib/python2.5/site-packages/numpy/core/records.py in 
__new__(subtype, shape, dtype, buf, offset, strides, formats, names, 
titles, byteorder, aligned)
249 if buf is None:
--> 250 self = ndarray.__new__(subtype, shape, (record, descr))
251 else:
252 self = ndarray.__new__(subtype, shape, (record, descr),

/home/mdroe/usr/lib/python2.5/site-packages/numpy/ma/mrecords.py in 
__array_finalize__(self, obj)
157 _fieldmask = getattr(obj, '_fieldmask', None)
158 if _fieldmask is None:
--> 159 mdescr = [(n,'|b1') for (n,_) in self.dtype.descr]
160 objmask = getattr(obj, '_mask', nomask)
161 if objmask is nomask:

<type 'exceptions.ValueError'>: too many values to unpack


In my own use case, I don't care if the individual elements of the 
nested array are maskable -- either the whole array being masked or not 
is good enough -- but perhaps that shortcoming is why this wasn't 
designed to work?  I have attached a patch to mrecords.py that gets me 
past this and does seem to allow the nested array to be masked as a whole.

Secondly, the "names" and "titles" kwargs seem to be ignored by the 
mrecarray constructor.


In [3]: x = mrecords.mrecarray(1, formats="f8", names="foo")

In [4]: x[0]['foo'] = 42.0
<type 'exceptions.ValueError'> Traceback (most recent call last)

/wonkabar/data1/scraps/<ipython console> in <module>()

/home/mdroe/usr/lib/python2.5/site-packages/numpy/ma/mrecords.py in 
__setitem__(self, indx, value)
309 def __setitem__(self, indx, value):
310 "Sets the given record to value."
--> 311 MaskedArray.__setitem__(self, indx, value)
312 if isinstance(indx, basestring):
313 self._mask[indx] = ma.getmaskarray(value)

/home/mdroe/usr/lib/python2.5/site-packages/numpy/ma/core.py in 
__setitem__(self, indx, value)
1437 # raise IndexError, msg
1438 if isinstance(indx, basestring):
-> 1439 ndarray.__setitem__(self._data, indx, value)
1440 ndarray.__setitem__(self._mask, indx, getmask(value))
1441 return

<type 'exceptions.ValueError'>: field named foo not found.


The included patch delegates these kwargs onto the underlying recarray.


Michael Droettboom
Science Software Branch
Operations and Engineering Division
Space Telescope Science Institute
Operated by AURA for NASA
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: mrecords.py.diff
Url: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080714/d31adb0e/attachment.pl 

More information about the Numpy-discussion mailing list