|
| 1 | +import operator |
| 2 | + |
1 | 3 | import numpy as np |
| 4 | +import pandas as pd |
2 | 5 | import pytest |
3 | 6 |
|
4 | 7 | import arkouda as ak |
@@ -378,6 +381,7 @@ def assert_indices(self, perm: pdarray, expected_py_indices): |
378 | 381 | def test_argsort_pdarray_float_ascending_nan_positions(self, na_position, expected): |
379 | 382 | a = ak.array([3.0, float("nan"), 1.0, 2.0]) |
380 | 383 | ea = ArkoudaExtensionArray(a) |
| 384 | + |
381 | 385 | perm = ea.argsort(ascending=True, na_position=na_position) |
382 | 386 | self.assert_indices(perm, expected) |
383 | 387 |
|
@@ -650,3 +654,63 @@ def test_copy_shallow_creates_new_wrapper_but_shares_data(self, ea): |
650 | 654 |
|
651 | 655 | # Values are equal |
652 | 656 | np.testing.assert_array_equal(shallow.to_numpy(), ea.to_numpy()) |
| 657 | + |
| 658 | + |
| 659 | +class TestArkoudaExtensionArrayArithmatic: |
| 660 | + @pytest.mark.parametrize( |
| 661 | + "op, np_op", |
| 662 | + [ |
| 663 | + (operator.add, operator.add), |
| 664 | + (operator.sub, operator.sub), |
| 665 | + (operator.mul, operator.mul), |
| 666 | + ], |
| 667 | + ) |
| 668 | + def test_arith_method_with_arkouda_array_operand(self, op, np_op): |
| 669 | + x = pd.array([1, 2, 3], dtype="ak_int64") |
| 670 | + y = pd.array([10, 20, 30], dtype="ak_int64") |
| 671 | + |
| 672 | + out = x._arith_method(y, op) |
| 673 | + |
| 674 | + assert type(out) is type(x) |
| 675 | + np.testing.assert_array_equal(out.to_numpy(), np_op(np.array([1, 2, 3]), np.array([10, 20, 30]))) |
| 676 | + |
| 677 | + @pytest.mark.parametrize( |
| 678 | + "op, scalar, expected", |
| 679 | + [ |
| 680 | + (operator.add, 5, np.array([6, 7, 8])), |
| 681 | + (operator.sub, 1, np.array([0, 1, 2])), |
| 682 | + (operator.mul, 2, np.array([2, 4, 6])), |
| 683 | + ], |
| 684 | + ) |
| 685 | + def test_arith_method_with_scalar_operand(self, op, scalar, expected): |
| 686 | + x = pd.array([1, 2, 3], dtype="ak_int64") |
| 687 | + |
| 688 | + out = x._arith_method(scalar, op) |
| 689 | + |
| 690 | + assert type(out) is type(x) |
| 691 | + np.testing.assert_array_equal(out.to_numpy(), expected) |
| 692 | + |
| 693 | + def test_arith_method_returns_notimplemented_for_unsupported_other(self): |
| 694 | + x = pd.array([1, 2, 3], dtype="ak_int64") |
| 695 | + |
| 696 | + # list is not scalar and not an Arkouda EA => NotImplemented |
| 697 | + out = x._arith_method([1, 2, 3], operator.add) |
| 698 | + assert out is NotImplemented |
| 699 | + |
| 700 | + def test_operator_add_raises_typeerror_for_unsupported_other(self): |
| 701 | + # This checks the user-visible behavior when NotImplemented propagates. |
| 702 | + x = pd.array([1, 2, 3], dtype="ak_int64") |
| 703 | + |
| 704 | + with pytest.raises(TypeError): |
| 705 | + _ = x + [1, 2, 3] |
| 706 | + |
| 707 | + def test_arith_method_unwraps_other_data_attribute(self): |
| 708 | + # Ensures the unwrap path is actually used. |
| 709 | + x = pd.array([1, 2, 3], dtype="ak_int64") |
| 710 | + y = pd.array([10, 20, 30], dtype="ak_int64") |
| 711 | + |
| 712 | + # Make sure y is an EA and has _data (the thing we unwrap). |
| 713 | + assert hasattr(y, "_data") |
| 714 | + |
| 715 | + out = x._arith_method(y, operator.add) |
| 716 | + np.testing.assert_array_equal(out.to_numpy(), np.array([11, 22, 33])) |
0 commit comments