[Numpy-discussion] surprising behavior of np.asarray on masked arrays

Eric Firing efiring at hawaii.edu
Fri Dec 6 01:53:48 EST 2013


On 2013/12/05 5:14 PM, Faraz Mirzaei wrote:
> Hi,
>
> If I pass a masked array through np.asarray, I get original unmasked array.
>
> Example:
>
> test = np.array([[1, 0], [-1, 3]])
>
> testMasked = ma.masked_less_equal(test, 0)
>
>
> print testMasked
>
> [[1 --]
>
>   [-- 3]]
>
>
> print testMasked.fill_value
>
> 999999
>
>
> print np.asarray(testMasked)
>
> [[ 1 0]
>
>   [-1 3]]
>
>
> Is this behavior intentional? How does the np.asarray access the
> original masked values? Shouldn't the masked values be at least filled
> with fill_value?

It might be nice, but it's not the way it is.  If you want to preserve 
masked arrays, use np.asanyarray() instead of np.asarray().  If you want 
to end up with filled ndarrays, use np.ma.filled().

Eric

>
>
> Thanks,
>
>
> Faraz
>
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>




More information about the NumPy-Discussion mailing list