Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

patricknaughton01
Copy link

@patricknaughton01 patricknaughton01 commented Feb 1, 2018

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
@@ -600,7 +600,7 @@ def filled(a, fill_value=None):
----------
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
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
Copy link
Member

eric-wieser commented Feb 3, 2018

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
Copy link
Member

charris commented Mar 1, 2018

@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
Copy link
Member

mattip commented Feb 26, 2020

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