Skip to content

BUG: Numpy ma diff skipping the mask when using argument 'prepend' #22465

@jsaladich

Description

@jsaladich

Describe the issue:

Hi ,

I have realized that when I pass a masked array in numpy ma diff np.ma.diff and prepend a masked intial value, the mask is deleted and replaced (in my case for 0. values). Despite being the fill_value different from 0.

I would expect that np.ma.diff keeps the mask after calculating the difference, specifically if you are prepending a masked array that keeps the same structure than the input array.
See below:

Reproduce the code example:

import numpy as np
# a is the dummy array
a = np.random.randn(5,10,10)
a = np.ma.masked_where(a<.1,a)
print(a.mask.any()) # returns True

b = np.ma.diff(a, prepend=a[:1,...],axis=0)
print(b.mask.any()) # returns False (unexpected)

# What I would expect / my workaround
c = np.ma.diff(np.ma.append(a[:1], a, axis=0),axis=0)
print(c.mask.any()) # returns True (as expected)

NumPy/Python version information:

My numpy version is: '1.23.4'
Python version is: 3.10.6

Context for the issue:

Shouldn't np.ma module's objects respect always the input mask (if it is properly referenced in its arguments)?

Thanks a lot and keep on with the amazing numpy!

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