MaskedArray __setitem__ for Record Values
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
On Friday 15 February 2008 10:51:04 Alexander Michael wrote:
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
Good call. The easiest is still to replace the line 1361 with: dval = narray(value, copy=False, dtype=self.dtype) The problem with the initial method was that the tuple got transformed into a (2,) array whose type could not be changed afterwards. With the new line, we directly transform value to a ndarray of the proper type. Mmh. Where should I commit the fix ? Directly to the trunk ?
On Fri, Feb 15, 2008 at 12:59 PM, Pierre GM <pgmdevlist@gmail.com> wrote:
Good call. The easiest is still to replace the line 1361 with: dval = narray(value, copy=False, dtype=self.dtype)
The problem with the initial method was that the tuple got transformed into a (2,) array whose type could not be changed afterwards. With the new line, we directly transform value to a ndarray of the proper type.
Even better- thanks!
Mmh. Where should I commit the fix ? Directly to the trunk ?
I hope so! Regards, Alex
participants (2)
-
Alexander Michael
-
Pierre GM