Skip to content

Feature Request: Implement pdarray.__pos__ (Unary Plus) in Arkouda #5116

Closed

Description

@ajpotts

Feature Request: Implement pdarray.__pos__ (Unary Plus) in Arkouda

Summary

Arkouda currently implements several unary operators for pdarray, but does not implement __pos__ (the unary plus operator, +a).
In Python and NumPy, unary plus is supported for array-like objects and acts as a no-op that preserves type and structure.

Adding pdarray.__pos__ improves API completeness, NumPy compatibility, and prevents surprising TypeError failures when user code or libraries apply unary plus.


Motivation

1. NumPy Compatibility

NumPy defines:

>>> import numpy as np
>>> a = np.array([1, -2, 3])
>>> +a
array([ 1, -2,  3])

Unary plus returns the same array with the same dtype.
Arkouda should match this behavior for interoperability and test consistency.

2. API Completeness

Arkouda supports:

  • __neg__ → unary -a
  • various binary operators

Missing:

  • __pos__ → unary +a

Unary plus is occasionally used in:

  • generic numeric code
  • broadcasting expressions
  • frameworks that validate or normalize operators
  • auto-generated symbolic expressions

Lack of __pos__ currently results in errors such as:

TypeError: bad operand type for unary +: 'pdarray'

3. Zero-cost implementation

Unary plus is semantically trivial:

  • No transformation needed
  • No backend operation required
  • Should simply return self

Proposed Behavior

Implement pdarray.__pos__ such that:

def __pos__(self):
    return self

This matches NumPy:

  • preserves dtype
  • preserves shape
  • performs no computation
  • returns a pdarray, not a copy

Example (expected behavior):

>>> import arkouda as ak
>>> a = ak.arange(5) * -1
>>> a
array([ 0, -1, -2, -3, -4])

>>> +a
array([ 0, -1, -2, -3, -4])

>>> (+a) is a
True   # unary plus should return the same object

Implementation Notes

File to modify:

arkouda/numpy/pdarrayclass.py

Add implementation to class pdarray:

def __pos__(self):
    return self

No server message required.


Tests

New unit tests should verify:

def test_unary_plus_pdarray():
    import arkouda as ak
    a = ak.arange(5)
    assert (+a is a)
    assert (+a).to_list() == [0, 1, 2, 3, 4]

NumPy alignment

Add unary-plus checks to the NumPy compatibility tests:

assert_arkouda_array_equivalent(+a_ak, +a_np)

Acceptance Criteria

  • +pdarray returns the same pdarray instance
  • Behavior matches NumPy unary plus semantics
  • No backend communication involved
  • No performance penalty
  • All NumPy-alignment tests pass after update
  • No regressions in negation / abs / binary operator behavior

Conclusion

Implementing pdarray.__pos__ is a simple, zero-cost enhancement that improves:

  • API completeness
  • NumPy interoperability
  • Predictability for users and libraries

This operator should be added for consistency and correctness.

Activity

  1. added this to the Numpy Alignment milestone on Dec 5, 2025
  2. added
    blocking
    on Dec 5, 2025
  3. self-assigned this
    on Dec 5, 2025
  4. added 3 commits that reference this issue on Dec 5, 2025
    dbeb7b6
    459bf5a
    a80eaa5
  5. added a commit that references this issue on Dec 19, 2025
    251d715
  6. added a commit that references this issue on Dec 22, 2025
    1c9801b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

blocking

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions