|
| 1 | +import unittest |
| 2 | +from enum import Enum |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +from arraykit import map_object |
| 6 | + |
| 7 | + |
| 8 | +class Color(Enum): |
| 9 | + R = 1 |
| 10 | + G = 2 |
| 11 | + |
| 12 | + |
| 13 | +# reference: prepare_iter_for_array's inference + build, as static_frame applies it |
| 14 | +_INEXACT = (float, complex, np.inexact) |
| 15 | +_BIG = 1_000_000_000_000_000 |
| 16 | + |
| 17 | + |
| 18 | +def _reference(arr, func): |
| 19 | + vals = [func(v) for v in arr] |
| 20 | + resolved = None |
| 21 | + has_str = has_non = has_inx = has_big = False |
| 22 | + for v in vals: |
| 23 | + vt = v.__class__ |
| 24 | + if vt is str or vt is np.str_ or vt is bytes or vt is np.bytes_: |
| 25 | + has_str = True |
| 26 | + elif hasattr(v, '__len__') or isinstance(v, Enum): |
| 27 | + resolved = object |
| 28 | + break |
| 29 | + else: |
| 30 | + has_non = True |
| 31 | + if vt in _INEXACT: |
| 32 | + has_inx = True |
| 33 | + elif vt is int and abs(v) > _BIG: |
| 34 | + has_big = True |
| 35 | + if (has_str and has_non) or (has_big and has_inx): |
| 36 | + resolved = object |
| 37 | + break |
| 38 | + return np.array(vals) if resolved is None else np.array(vals, dtype=object) |
| 39 | + |
| 40 | + |
| 41 | +class TestUnit(unittest.TestCase): |
| 42 | + def _check(self, arr, func): |
| 43 | + post = map_object(arr, func) |
| 44 | + exp = _reference(arr, func) |
| 45 | + self.assertEqual(post.dtype, exp.dtype, (arr.dtype, exp.dtype)) |
| 46 | + self.assertTrue(np.array_equal(post, exp)) |
| 47 | + self.assertFalse(post.flags.writeable) |
| 48 | + return post |
| 49 | + |
| 50 | + def test_map_object_str_from_float(self) -> None: |
| 51 | + post = self._check(np.array([1.5, 2.25, 3.0]), lambda x: str(x)) |
| 52 | + self.assertEqual(post.dtype.kind, 'U') |
| 53 | + |
| 54 | + def test_map_object_str_from_bool(self) -> None: |
| 55 | + post = self._check(np.array([True, False, True]), lambda x: str(x)) |
| 56 | + self.assertEqual(post.tolist(), ['True', 'False', 'True']) |
| 57 | + self.assertEqual(post.dtype.kind, 'U') |
| 58 | + |
| 59 | + def test_map_object_str_from_int(self) -> None: |
| 60 | + self._check(np.array([1, 2, 3], dtype=np.int64), lambda x: str(x)) |
| 61 | + |
| 62 | + def test_map_object_native_float(self) -> None: |
| 63 | + post = self._check(np.array([1.5, 2.5]), lambda x: float(x) * 2) |
| 64 | + self.assertEqual(post.dtype, np.dtype(np.float64)) |
| 65 | + |
| 66 | + def test_map_object_native_int(self) -> None: |
| 67 | + post = self._check(np.array([1, 2, 3]), lambda x: int(x) + 1) |
| 68 | + self.assertEqual(post.dtype, np.dtype(np.int64)) |
| 69 | + |
| 70 | + def test_map_object_tuple_result(self) -> None: |
| 71 | + post = self._check(np.array([1, 2]), lambda x: (int(x), int(x))) |
| 72 | + self.assertEqual(post.dtype, np.dtype(object)) |
| 73 | + |
| 74 | + def test_map_object_list_result(self) -> None: |
| 75 | + post = self._check(np.array([1, 2]), lambda x: [int(x)]) |
| 76 | + self.assertEqual(post.dtype, np.dtype(object)) |
| 77 | + |
| 78 | + def test_map_object_mixed_str_nonstr(self) -> None: |
| 79 | + post = self._check(np.array([1, 2, 3]), lambda x: str(x) if x > 1 else int(x)) |
| 80 | + self.assertEqual(post.dtype, np.dtype(object)) |
| 81 | + |
| 82 | + def test_map_object_python_float(self) -> None: |
| 83 | + self._check(np.array([1, 2, 3]), lambda x: 1.5) |
| 84 | + |
| 85 | + def test_map_object_bigint_and_inexact(self) -> None: |
| 86 | + # a large python int mixed with a python float -> object |
| 87 | + post = self._check(np.array([1, 2]), lambda x: 10**18 if x == 1 else 1.5) |
| 88 | + self.assertEqual(post.dtype, np.dtype(object)) |
| 89 | + |
| 90 | + def test_map_object_bigint_only(self) -> None: |
| 91 | + # big ints alone (no inexact) do not force object |
| 92 | + post = self._check(np.array([1, 2]), lambda x: 10**18) |
| 93 | + self.assertNotEqual(post.dtype, np.dtype(object)) |
| 94 | + |
| 95 | + def test_map_object_enum_result(self) -> None: |
| 96 | + post = self._check(np.array([1, 2]), lambda x: Color.R) |
| 97 | + self.assertEqual(post.dtype, np.dtype(object)) |
| 98 | + |
| 99 | + def test_map_object_object_input(self) -> None: |
| 100 | + arr = np.array(['a', 'bb', 'ccc'], dtype=object) |
| 101 | + post = self._check(arr, lambda x: len(x)) |
| 102 | + self.assertEqual(post.tolist(), [1, 2, 3]) |
| 103 | + |
| 104 | + def test_map_object_receives_numpy_scalar(self) -> None: |
| 105 | + # elements are boxed as numpy scalars, matching Series/array iteration |
| 106 | + seen = [] |
| 107 | + map_object(np.array([1.5, 2.5]), lambda x: seen.append(type(x)) or x) |
| 108 | + self.assertTrue(all(t is np.float64 for t in seen)) |
| 109 | + |
| 110 | + def test_map_object_str_subclass_is_object(self) -> None: |
| 111 | + # a str subclass is not an exact str -> sized object -> object array |
| 112 | + class S(str): |
| 113 | + pass |
| 114 | + |
| 115 | + post = map_object(np.array([1, 2]), lambda x: S(str(x))) |
| 116 | + self.assertEqual(post.dtype, np.dtype(object)) |
| 117 | + |
| 118 | + def test_map_object_empty(self) -> None: |
| 119 | + post = self._check(np.array([], dtype=np.float64), lambda x: str(x)) |
| 120 | + self.assertEqual(len(post), 0) |
| 121 | + |
| 122 | + def test_map_object_propagates_exception(self) -> None: |
| 123 | + def bad(x): |
| 124 | + raise ValueError('boom') |
| 125 | + |
| 126 | + with self.assertRaises(ValueError): |
| 127 | + map_object(np.array([1, 2]), bad) |
| 128 | + |
| 129 | + def test_map_object_errors(self) -> None: |
| 130 | + with self.assertRaises(ValueError): # 2d |
| 131 | + map_object(np.array([[1, 2]]), lambda x: x) |
| 132 | + with self.assertRaises(TypeError): # not callable |
| 133 | + map_object(np.array([1, 2]), 3) |
| 134 | + with self.assertRaises(TypeError): # not an array |
| 135 | + map_object([1, 2], lambda x: x) |
| 136 | + |
| 137 | + |
| 138 | +if __name__ == '__main__': |
| 139 | + unittest.main() |
0 commit comments