array += masked_array gives a normal array, not masked array
Ok, so you guys shot down my last attempt at finding a bug :). Here's another attempt. array + masked_array outputs a masked array array += masked_array outputs an array. I'm actually not sure if this is a bug (works the same for both the old and new masked arrays), but I thought this was unexpected. *However*, if the right side were an old masked array, the masked values would not be added to the output array. With the new masked arrays, the data from the masked array is added regardless of whether the value is masked (example, below). -Tony Example: ======= In [1]: import numpy In [2]: normal = numpy.array([1, 1]) In [3]: masked = numpy.ma.array([1, 1], mask=[True, False]) In [4]: new_masked = normal + masked In [5]: new_masked Out[5]: masked_array(data = [-- 2], mask = [ True False], fill_value=999999) In [6]: normal += masked In [7]: normal Out[7]: array([2, 2]) # If used old masked arrays in the above, the final output would be: array([1, 2])
On Sunday 01 June 2008 11:22:05 Tony Yu wrote:
array + masked_array outputs a masked array
That's expected: you create a new object from two objects (array and masked_array), the latter has a higher priority than the former, you end up with a masked array
array += masked_array outputs an array.
That's expected as well: you modify the values of array in place, but don't modify its type. Similarly, masked_array += array gives a masked array.
*However*, if the right side were an old masked array, the masked values would not be added to the output array. With the new masked arrays, the data from the masked array is added regardless of whether the value is masked (example, below).
With oldnumeric.ma, masked_array has to be converted to a ndarray. During the conversion, the masked data are filled with a value that depends on the context of the call: here, the function called is add, and the corresponding filling value is 0. Your line normal += masked_array is then equivalent to normal += [0,1] With numpy.ma, masked_array is already recognized as a ndarray, and no conversion takes place. In particular, the values of masked_array are not filled. You should use normal += masked_array.filled(0)
participants (2)
-
Pierre GM -
Tony Yu