surprising behavior of np.asarray on masked arrays

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

surprising behavior of np.asarray on masked arrays

Faraz Mirzaei
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?


Thanks,


Faraz


_______________________________________________
NumPy-Discussion mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: surprising behavior of np.asarray on masked arrays

Stéfan van der Walt
Hi Faraz

On Thu, 05 Dec 2013 19:14:01 -0800, Faraz Mirzaei wrote:
> If I pass a masked array through np.asarray, I get original unmasked array.

`asarray` disregards any information attached to the underlying ndarray by the
subclass.  To preserve the subclass, you'd need to use `asanyarray`.

The only functions that are aware of masked arrays live inside of `np.ma`, so
you can also use `np.ma.asarray`.

Which behavior in particular would you like to see, since I presume you can
already get hold of the filled array, should you want to?

Stéfan

_______________________________________________
NumPy-Discussion mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: surprising behavior of np.asarray on masked arrays

Eric Firing
In reply to this post by Faraz Mirzaei
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
> [hidden email]
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>

_______________________________________________
NumPy-Discussion mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/numpy-discussion