Skip to content

BUG: numpy.ma.MaskedArray arithmetic with Python scalars differs from ndarray #31868

Description

@whyvineet

Describe the issue:

When you use binary arithmetic with Python scalars, numpy.ma.MaskedArray gives different results compared to numpy.ndarray.

For example, if you add a Python integer to an int8 ndarray, the array keeps its dtype. But if you do the same with a MaskedArray, the result changes to int64. This also happens with other binary operations like exponentiation.

This occurs even when the MaskedArray has no masked elements, so the only difference between the two inputs is that one is an ndarray and the other is a MaskedArray.

Since numpy.ma is documented as a "nearly work-alike replacement" for NumPy and MaskedArray is a subclass of ndarray, I expected equivalent arithmetic operations to produce the same dtype and values when no elements are masked.

I confirmed this behavior on both NumPy 2.5.1 and the current development branch.

Reproduce the code example:

import numpy as np
import numpy.ma as ma

arr = np.array([100], dtype=np.int8)
masked = ma.masked_array([100], dtype=np.int8)

print("ndarray:", arr + 50, (arr + 50).dtype) # ndarray: [-106] int8
print("MaskedArray:", masked + 50, (masked + 50).dtype) # MaskedArray: [150] int64

arr = np.array([2], dtype=np.int8)
masked = ma.masked_array([2], dtype=np.int8)

print("ndarray:", arr ** 10, (arr ** 10).dtype) # ndarray: [0] int8
print("MaskedArray:", masked ** 10, (masked ** 10).dtype) # MaskedArray: [1024] int64

Error message:

Python and NumPy Versions:

2.5.1
3.15.0a6 (tags/v3.15.0a6:15b216f, Feb 11 2026, 12:54:03) [MSC v.1944 64 bit (AMD64)]

Runtime Environment:

No response

How does this issue affect you or how did you find it:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions