Skip to content

Augmented assignments between unmasked and masked arrays #9270

@mwtoews

Description

@mwtoews

I'm seeing slightly different and unexpected behavior with unmasked and masked arrays in augmented assignments.

For example, the result of masked += unmasked is expected:

import numpy as np

def ab():
    return np.array([1, 1]), np.ma.array([5, 5], mask=[False, True])

a, b = ab()
print('a: {0}; b: {1}; b.data: {2}'.format(a, b, b.data))
# a: [1 1]; b: [5 --]; b.data: [5 5]

b += a
print('b: {0}; b.data: {1}'.format(b, b.data))
# b: [6 --]; b.data: [6 5]

however the augmented unmasked += masked applies the operation to all elements, regardless of any masked values:

a, b = ab()
a += b
print('a: {0}'.format(a))
# a: [6 6]

I would have expected a: [6 5], similar to b.data shown above. Similar behavior is found with different augmented operators.

And as a side note, I get different behavior for unmasked + masked with different versions:

a, b = ab()
c = a + b
print('{0}: c: {1}; c.data: {2}'.format(np.__version__, c, c.data))

Testing a few versions that I have on-hand:

1.4.1: c: [6 --]; c.data: [6 5]
1.11.3: c: [6 --]; c.data: [6 1]

It's a minor detail, but an odd one why these are different. The older NumPy behavior is similar to first augmented example which works as expected for both of these NumPy versions tested.

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