[Numpy-discussion] MaskedArray __setitem__ for Record Values

Alexander Michael lxander.m at gmail.com
Fri Feb 15 10:51:04 EST 2008


I am attempting to work with field dtypes (i.e. record arrays) as
MaskedArrays. I don't want to use MaskedRecords because I want the
mask to apply to the whole record since the first field is primary and
the rest are "tag along" values associated with the primary data
value. The numpy.ndarray supports the ability to set a record with a
(heterogenous) sequence, but MaskedArray does not:

>>> import numpy
>>> from numpy import ma

>>> d = numpy.empty((5,4), dtype=[('value', float),('source', int)])
>>> a = ma.MaskedArray(d, mask=True, fill_value=(0.0,0))
>>> a[0,0] = (10.0, 1)

numpy\ma\core.py in __setitem__(self, indx, value)
   1359             return
   1360         #....
-> 1361         dval = getdata(value).astype(self.dtype)
   1362         valmask = getmask(value)
   1363         if self._mask is nomask:

TypeError: expected a readable buffer object

I suggest replacing " dval = getdata(value).astype(self.dtype)" with
something along the lines of:

        dval = None
        if self.dtype.fields is None:
            dval = getdata(value).astype(self.dtype)
        else:
            dval = value

but I do not understand all of the activity in the __setitem__ method,
so this may be too naive
a fix (but it *appears* to work).

Thanks,
Alex



More information about the NumPy-Discussion mailing list