[SciPy-dev] [Numpy-discussion] return type of ndarray + ma.array

Travis Oliphant oliphant.travis at ieee.org
Fri Jan 6 00:27:34 EST 2006


Alexander Belopolsky wrote:

> the following session demonstrates the difference between numpy.ma  
> and old MA behavior.
>
> Sum ndarray and ma.array is ndarray
>
> >>> type(numeric.array([1])+ma.array([1]))
> <type 'numpy.ndarray'>
>
> However, taken in the different order,
> >>> type(ma.array([1])+numeric.array([1]))
> <class 'numpy.core.ma.MaskedArray'>
>
> In the old MA module the sum is MaskedArray in any order:
> >>> type(MA.array([1])+Numeric.array([1]))
> <class 'MA.MaskedArray'>
>
> >>> type(Numeric.array([1])+MA.array([1]))
> <class 'MA.MaskedArray'>
>
> I suspected that new ma module may not have __radd__ defined, but it  
> does.
>
> Can anyone explain this?

I'm not sure.  Because Numeric.add(Numeric.array([1]), MA.array([1])) 
works.  I don't know why it's never called...

But, the __array_wrap__ method should help here.

I defined the __array_wrap__ method in ma.py so this will work now as 
expected.    The ufunc does hand off calculation to an object that has 
an __radd__ method (but only if that object can only be converted to an 
*object* numpy array).   We could change this, but I don't want to check 
for an __radd__ method all the time.  This would slow ufuncs down. 

I don't think this is a problem, since the __array_wrap__ method uses 
the mask of self.  This does not work quite right on ufuncs with more 
than 2 inputs, but I don't think masked array supports those anyway...

-Travis









More information about the SciPy-Dev mailing list