|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
| 3 | +import numpy as np |
3 | 4 | import pytest
|
4 | 5 |
|
5 | 6 | import pandas as pd
|
@@ -128,11 +129,13 @@ class BaseComparisonOpsTests(BaseOpsUtil):
|
128 | 129 | """Various Series and DataFrame comparison ops methods."""
|
129 | 130 |
|
130 | 131 | def _compare_other(self, s, data, op_name, other):
|
| 132 | + |
131 | 133 | op = self.get_op_from_name(op_name)
|
132 |
| - if op_name == "__eq__": |
133 |
| - assert not op(s, other).all() |
134 |
| - elif op_name == "__ne__": |
135 |
| - assert op(s, other).all() |
| 134 | + if op_name in ["__eq__", "__ne__"]: |
| 135 | + # comparison should match point-wise comparisons |
| 136 | + result = op(s, other) |
| 137 | + expected = s.combine(other, op) |
| 138 | + self.assert_series_equal(result, expected) |
136 | 139 |
|
137 | 140 | else:
|
138 | 141 |
|
@@ -182,3 +185,24 @@ def test_invert(self, data):
|
182 | 185 | result = ~s
|
183 | 186 | expected = pd.Series(~data, name="name")
|
184 | 187 | self.assert_series_equal(result, expected)
|
| 188 | + |
| 189 | + @pytest.mark.parametrize("ufunc", [np.positive, np.negative, np.abs]) |
| 190 | + def test_unary_ufunc_dunder_equivalence(self, data, ufunc): |
| 191 | + # the dunder __pos__ works if and only if np.positive works, |
| 192 | + # same for __neg__/np.negative and __abs__/np.abs |
| 193 | + attr = {np.positive: "__pos__", np.negative: "__neg__", np.abs: "__abs__"}[ |
| 194 | + ufunc |
| 195 | + ] |
| 196 | + |
| 197 | + exc = None |
| 198 | + try: |
| 199 | + result = getattr(data, attr)() |
| 200 | + except Exception as err: |
| 201 | + exc = err |
| 202 | + |
| 203 | + # if __pos__ raised, then so should the ufunc |
| 204 | + with pytest.raises((type(exc), TypeError)): |
| 205 | + ufunc(data) |
| 206 | + else: |
| 207 | + alt = ufunc(data) |
| 208 | + self.assert_extension_array_equal(result, alt) |
0 commit comments