Skip to content

Bug: ak.square does not support bool dtype (NumPy mismatch) #5250

@ajpotts

Description

@ajpotts

Bug: ak.square does not support bool dtype (NumPy mismatch)

Summary

arkouda.numpy.square raises a TypeError when called on boolean arrays,
while NumPy defines np.square for bool by treating values as integers
(False → 0, True → 1).

This mismatch was discovered via the NumPy-alignment test suite.


Expected Behavior (NumPy)

np.square(np.array([True, False, True], dtype=bool))
# array([1, 0, 1])

NumPy supports square for boolean inputs by applying integer semantics.


Actual Behavior (Arkouda)

ak.square(ak.array([True, False, True]))
# TypeError: square only implements types [numpy.float64, numpy.int64, numpy.uint64]

The error originates from client-side dtype validation.


Failing Test

tests/numpy/alignment_verification/numeric_alignment_numpy.py::
test_unary_alignment[False-square-square-square-kinds16]

Traceback excerpt:

TypeError: square only implements types [<class 'numpy.float64'>,
<class 'numpy.int64'>, <class 'numpy.uint64'>]

Root Cause

ak.square dispatches through _general_helper, which performs a strict
dtype check via _datatype_check and explicitly excludes bool.

This is a client-side restriction rather than a backend limitation.


Proposed Fix

Align with NumPy by supporting boolean inputs. Two viable approaches:

  1. Client-side cast (recommended)
    If input dtype is bool, cast to int64 (or uint64) and apply square.

  2. Relax dtype gate
    Extend _datatype_check to allow bool and ensure correct backend handling.

Both approaches preserve NumPy semantics (True → 1, False → 0).


Why This Matters

  • Required for NumPy semantic alignment
  • Prevents unnecessary TypeError in vectorized pipelines
  • Improves compatibility with NumPy- and pandas-style code
  • Allows alignment tests to run without xfail

Environment

  • Arkouda client: 2025.12.x
  • Arkouda server: 2025.12.x
  • Python: 3.13

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions