Skip to content

DOC: Document ma.filled behavior with non-scalar fill_value. - #10504

Closed
patricknaughton01 wants to merge 1 commit into
numpy:masterfrom
patricknaughton01:issue4363_ma.filled
Closed

patricknaughton01 wants to merge 1 commit into
numpy:masterfrom
patricknaughton01:issue4363_ma.filled

Conversation

@patricknaughton01

@patricknaughton01 patricknaughton01 commented Feb 1, 2018 •

Copy link
Copy Markdown

Add documentation to numpy.ma.filled to indicate that fill_value can
take on any type that can be broadcast to the same type as a.

Closes #4363

Add documentation to numpy.ma.filled to indicate that `fill_value` can
take on any type that can be broadcast to the same type as `a`.
@charris charris changed the title DOC: See Issue 4363 DOC: Document ma.filled behavior with non-scalar fill_value. Feb 3, 2018
Comment thread numpy/ma/core.py
a : MaskedArray or array_like
An input object.
fill_value : scalar, optional
fill_value : scalar or shape that can be broadcast to same shape as `a`, optional

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't very clear. Maybe something like

    fill_value : {None, array_like}, optional
        If array_like, the resulting ndarray must be broadcastable
        over `a`. The default is None.

@eric-wieser

eric-wieser commented Feb 3, 2018 •

Copy link
Copy Markdown
Member

I'm wary of documenting this if it's not deliberate or well-supported behavior.

Arrays with non-scalar fill values also behave poorly upon slicing:

>>> m = np.ma.array(np.zeros((3, 5)), mask=True, fill_value=np.arange(5))
>>> m.filled()
array([[0., 1., 2., 3., 4.],
       [0., 1., 2., 3., 4.],
       [0., 1., 2., 3., 4.]])
>>> m[:, :2].filled()
ValueError: could not broadcast input array from shape (5) into shape (3,2)

Fixing this requires copying parts of xarray, which seems super out of scope.

I think it would be better to just advise using

np.where(m.mask, some_broadcasting_fillvalue, m.data)

possibly even in a deprecation warning.

@charris

charris commented Mar 1, 2018

Copy link
Copy Markdown
Member

@eric-wieser Hmm, the handling of scalars and masked_singleton are not array fill value safe, other bits look intended to work with arrays. As it currently stands, array_like fill values are not safe and should not be documented. The function needs some work.

@mattip

mattip commented Feb 26, 2020

Copy link
Copy Markdown
Member

Closing, this has been fixed in gh-13698.

@mattip mattip closed this Feb 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DOC: Document ma.filled behavior with non-scalar fill_value

4 participants