Skip to content

BUG: np.positive ufunc and unary + operator fail on boolean arrays, while binary arithmetic works #32100

Description

@qiqicliff

Describe the issue:

The unary ufunc np.positive and the unary + operator raise a UFuncNoLoopError when applied to a boolean NumPy array. This is inconsistent with:

Python's built‑in semantics, where +True returns 1 and +False returns 0.

NumPy's binary arithmetic operations (e.g., np.add, + between arrays, np.multiply), which treat boolean values as integers (True → 1, False → 0) and work without error.

The positive ufunc is the identity operation – it should simply return the input unchanged (after casting to a numeric type). There is no mathematical reason to reject booleans, and the fact that binary operators accept them makes this omission surprising for users.

Reproduce the code example:

import numpy as np

b = np.array([True, False])

# Binary arithmetic works as expected:
print(np.add(b, 0))        # [1 0]
print(b + 0)               # [1 0]
print(np.multiply(b, 1))   # [1 0]

# These fail with the same error:
try:
    print(np.positive(b))
except TypeError as e:
    print("np.positive:", e)

try:
    print(+b)
except TypeError as e:
    print("unary +:", e)

Error message:

[1 0]
[1 0]
[1 0]
np.positive: ufunc 'positive' did not contain a loop with signature matching types <class 'numpy.dtypes.BoolDType'> -> None
unary +: ufunc 'positive' did not contain a loop with signature matching types <class 'numpy.dtypes.BoolDType'> -> None

Python and NumPy Versions:

Numpy 1.26.4
Python 3.8

Runtime Environment:

No response

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

No response

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