Repository navigation
Description
Bug: ak.union1d not NumPy-aligned for empty-input cases (returns non-unique / unsorted)
Summary
ak.union1d does not match numpy.union1d semantics when one input is empty. In these cases, Arkouda returns the non-empty input as-is (preserving duplicates and original order) instead of returning a sorted unique union.
This breaks NumPy alignment expectations and causes failures in the alignment verification suite.
Expected behavior (NumPy)
numpy.union1d(a, b) returns the sorted, unique values that are in either a or b, even if one of the inputs is empty.
Example:
np.union1d([], [20, 19, 4, 4, 4, 17, 2, 18, 3, 4])
→[2, 3, 4, 17, 18, 19, 20]
Actual behavior (Arkouda)
When one input is empty, ak.union1d returns the non-empty input without applying uniqueness or sorting.
Example observed:
ak.union1d([], b)returnsbunchanged:
→[20, 19, 4, 4, 4, 17, 2, 18, 3, 4](size 10)
instead of[2, 3, 4, 17, 18, 19, 20](size 7)
Reproduction
Test
tests/numpy/alignment_verification/pdarraysetops_alignment.py::test_union1d_matches_numpy
Failing parameterizations
test_union1d_matches_numpy[0-10-int64]test_union1d_matches_numpy[0-10-uint64]test_union1d_matches_numpy[10-0-int64]test_union1d_matches_numpy[10-0-uint64]
(Other non-empty/non-empty cases pass, suggesting an empty-input fast path that bypasses unique/sort.)
Environment
- Client warning observed during test run:
Version mismatch between client (2025.12.16+27...) and server (2025.12.16+26...)- While this may affect some behaviors, the failure pattern strongly suggests an empty-input code path omission in
union1d.
Likely root cause
union1d appears to have an early-return optimization like:
if a.size == 0: return bif b.size == 0: return a
which bypasses the normal unique + sort behavior.
Proposed fix
Ensure union1d always returns sorted uniques, including empty-input cases. For empty fast paths, apply unique (and sort if needed) before returning:
- If
ar1.size == 0: returnsort(unique(ar2)) - If
ar2.size == 0: returnsort(unique(ar1))
Or unify logic by always computing unique(concatenate([ar1, ar2])) (and sorting if unique is not guaranteed sorted).
Acceptance criteria
ak.union1dmatchesnp.union1dfor:- one empty, one non-empty (both orders)
- both empty
- both non-empty
- int64 and uint64
- Alignment test suite passes without xfails for
union1d.
Activity
Closes #5272: alignment tests for arkouda.numpy.pdarraysetops (#5274)