Skip to content

np.pad applied to the MaskedArray subclass silently unmasks the array, and returns output as ndarray #8881

@lukelbd

Description

@lukelbd

It seems to me this should be resolved so that the MaskedArray subclass is preserved and the mask attribute is likewise padded -- np.repeat and np.tile do this, for example. I suppose I see the complication -- for masking modes like constant, whether to mask these new values is ambiguous; perhaps in this case an explicit warning should be issued. But for methods that sample the edges of the existing MaskedArray, it seems to me it would make more sense to preserve the mask; perhaps this all requires a new np.ma.pad module.

Here is a simple example

In [1]: import numpy as np

In [2]: a = np.ma.MaskedArray([-999,-999,0,1,2],mask=[True,True,False,False,False],fill_value=999)

In [3]: a
Out[3]:
masked_array(data = [-- -- 0 1 2],
             mask = [ True  True False False False],
       fill_value = 999)

In [4]: np.pad(a,0,'wrap') # note a.filled() is NOT invoked; instead, object is simply unmasked
Out[4]: array([-999, -999,    0,    1,    2])

In [6]: np.pad(a,0,'edge') # same results as above; same is true for every padding 'mode'
Out[6]: array([-999, -999,    0,    1,    2])

In [8]: np.repeat(a,1) # expected behavior
Out[8]:
masked_array(data = [-- -- 0 1 2],
             mask = [ True  True False False False],
       fill_value = 999)

In [9]: np.tile(a,1) # expected behavior
Out[9]:
masked_array(data = [-- -- 0 1 2],
             mask = [ True  True False False False],
       fill_value = 999)

I noticed there are several other inconsistencies here -- for example, there are np.repeat and np.ma.repeat modules that do the same thing; there is only an np.tile method and no np.ma.tile method (but np.tile works as expected); and there is an np.concatenate method and np.ma.concatenate method where the former has unexpected behavior (sets mask=False, unmasks the data, and changes fill_value).

Perhaps I should start a more general thread on these inconsistency issues elsewhere?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions