Skip to content

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

Closed

Description

@abalkin

The ma.filled function and equivalent MaskedArray method are documented as follows:

filled(self, fill_value=None) ..

    Parameters
    ----------
    fill_value : scalar, optional

However, the current implementation (using np.copyto(.., where=mask)) allows any shape that can be broadcast to the shape of self for fill_value.

This is a useful property and should be documented.

Activity

  1. ay11111111111 commented on Mar 31, 2019

    @ay11111111111
    Contributor

    HI! I am an Outreachy applicant and I guess, this is the only unassigned issue :) Can I solve it?

  2. mattip commented on Mar 31, 2019

    @mattip
    Member

    @yesass yes, and please xref your pull request by putting #4363 in the comments so others will know you have started working on this.

  3. added a commit that references this issue on Apr 2, 2019
  4. ay11111111111 commented on Apr 2, 2019

    @ay11111111111
    Contributor

    @mattip Hi! Can you check it out? #13241

  5. eric-wieser commented on Apr 2, 2019

    @eric-wieser
    Member

    Lifting a comment from #10504:

    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.

  6. ay11111111111 commented on Apr 2, 2019

    @ay11111111111
    Contributor

    @eric-wieser Hello!
    So as I understood, the doc should be this way:

            fill_value : {None, array_like}, optional
            If array_like, the resulting ndarray must be broadcastable
            over `a`.  Arrays with non-scalar fill values are deprecated. The default is None.
    

    Also, there should be Deprecation Warning raised like:

    warnings.warn(
                    "Arrays with non-scalar fill values are deprecated. Use "
                    "Arrays with scalar values instead".format(self.__name__),
                    DeprecationWarning, stacklevel=2)
    
  7. eric-wieser commented on Apr 2, 2019

    @eric-wieser
    Member

    I think we should compromise on allowing filled(...) to take a broadcasting value, and writing tests for it (it's easy to get right), but deprecating using an non-scalar fill value for fill_value (because getting that right is hard).

  8. mattip commented on Apr 2, 2019

    @mattip
    Member

    The deprecation warning should have a comment before it: # 2019-04-02, 1.17.0, gh-4363

  9. ay11111111111 commented on Apr 2, 2019

    @ay11111111111
    Contributor

    Ok, I will edit the docstring and add deprecation warning. I have several questions:
    Is it necessary to add examples in docstrings?
    And should I write a test? If yes, could you tell a specific file that contains tests in this method (to see the examples).

  10. mattip commented on Apr 2, 2019

    @mattip
    Member

    It is nice but not required to add examples in the docstrings. Some of them already have examples. If you do modify docstrings, make sure to run python runtests.py --refguide-check in addition to python runtests.py to make sure they pass tests.

    You should always add tests for changed behaviour. Deprecation tests go into /numpy/ma/tests/test_deprecations.py, regulare masked array tests go in other files in that directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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