|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +from pandas._libs import index as libindex |
| 4 | + |
| 5 | + |
| 6 | +class TestNumericEngine(object): |
| 7 | + def test_is_monotonic(self, numeric_indexing_engine_type_and_dtype): |
| 8 | + engine_type, dtype = numeric_indexing_engine_type_and_dtype |
| 9 | + |
| 10 | + num = 1000 |
| 11 | + arr = np.array([1] * num + [2] * num + [3] * num, dtype=dtype) |
| 12 | + |
| 13 | + # monotonic increasing |
| 14 | + engine = engine_type(lambda: arr, len(arr)) |
| 15 | + assert engine.is_monotonic_increasing |
| 16 | + assert not engine.is_monotonic_decreasing |
| 17 | + |
| 18 | + # monotonic decreasing |
| 19 | + engine = engine_type(lambda: arr[::-1], len(arr)) |
| 20 | + assert not engine.is_monotonic_increasing |
| 21 | + assert engine.is_monotonic_decreasing |
| 22 | + |
| 23 | + # neither monotonic increasing or decreasing |
| 24 | + arr = np.array([1] * num + [2] * num + [1] * num, dtype=dtype) |
| 25 | + engine = engine_type(lambda: arr[::-1], len(arr)) |
| 26 | + assert not engine.is_monotonic_increasing |
| 27 | + assert not engine.is_monotonic_decreasing |
| 28 | + |
| 29 | + def test_is_unique(self, numeric_indexing_engine_type_and_dtype): |
| 30 | + engine_type, dtype = numeric_indexing_engine_type_and_dtype |
| 31 | + |
| 32 | + # unique |
| 33 | + arr = np.array([1, 2, 4, 3], dtype=dtype) |
| 34 | + engine = engine_type(lambda: arr, len(arr)) |
| 35 | + assert engine.is_unique |
| 36 | + |
| 37 | + # not unique |
| 38 | + arr = np.array([1, 2, 1], dtype=dtype) |
| 39 | + engine = engine_type(lambda: arr, len(arr)) |
| 40 | + assert not engine.is_unique |
| 41 | + |
| 42 | + def test_get_loc(self, numeric_indexing_engine_type_and_dtype): |
| 43 | + engine_type, dtype = numeric_indexing_engine_type_and_dtype |
| 44 | + |
| 45 | + # unique |
| 46 | + arr = np.array([1, 2, 3], dtype=dtype) |
| 47 | + engine = engine_type(lambda: arr, len(arr)) |
| 48 | + assert engine.get_loc(2) == 1 |
| 49 | + |
| 50 | + # monotonic |
| 51 | + num = 1000 |
| 52 | + arr = np.array([1] * num + [2] * num + [3] * num, dtype=dtype) |
| 53 | + engine = engine_type(lambda: arr, len(arr)) |
| 54 | + assert engine.get_loc(2) == slice(1000, 2000) |
| 55 | + |
| 56 | + # not monotonic |
| 57 | + arr = np.array([1, 2, 3] * num, dtype=dtype) |
| 58 | + engine = engine_type(lambda: arr, len(arr)) |
| 59 | + expected = np.array([False, True, False] * num, dtype=bool) |
| 60 | + result = engine.get_loc(2) |
| 61 | + assert (result == expected).all() |
| 62 | + |
| 63 | + |
| 64 | +class TestObjectEngine(object): |
| 65 | + engine_type = libindex.ObjectEngine |
| 66 | + dtype = np.object_ |
| 67 | + |
| 68 | + def test_is_monotonic(self): |
| 69 | + |
| 70 | + num = 1000 |
| 71 | + arr = np.array(['a'] * num + ['a'] * num + ['c'] * num, |
| 72 | + dtype=self.dtype) |
| 73 | + |
| 74 | + # monotonic increasing |
| 75 | + engine = self.engine_type(lambda: arr, len(arr)) |
| 76 | + assert engine.is_monotonic_increasing |
| 77 | + assert not engine.is_monotonic_decreasing |
| 78 | + |
| 79 | + # monotonic decreasing |
| 80 | + engine = self.engine_type(lambda: arr[::-1], len(arr)) |
| 81 | + assert not engine.is_monotonic_increasing |
| 82 | + assert engine.is_monotonic_decreasing |
| 83 | + |
| 84 | + # neither monotonic increasing or decreasing |
| 85 | + arr = np.array(['a'] * num + ['b'] * num + ['a'] * num, |
| 86 | + dtype=self.dtype) |
| 87 | + engine = self.engine_type(lambda: arr[::-1], len(arr)) |
| 88 | + assert not engine.is_monotonic_increasing |
| 89 | + assert not engine.is_monotonic_decreasing |
| 90 | + |
| 91 | + def test_is_unique(self): |
| 92 | + # unique |
| 93 | + arr = np.array(['a', 'b', 'c', 'd'], dtype=self.dtype) |
| 94 | + engine = self.engine_type(lambda: arr, len(arr)) |
| 95 | + assert engine.is_unique |
| 96 | + |
| 97 | + # not unique |
| 98 | + arr = np.array(['a', 'b', 'a'], dtype=self.dtype) |
| 99 | + engine = self.engine_type(lambda: arr, len(arr)) |
| 100 | + assert not engine.is_unique |
| 101 | + |
| 102 | + def test_get_loc(self): |
| 103 | + # unique |
| 104 | + arr = np.array(['a', 'b', 'c'], dtype=self.dtype) |
| 105 | + engine = self.engine_type(lambda: arr, len(arr)) |
| 106 | + assert engine.get_loc('b') == 1 |
| 107 | + |
| 108 | + # monotonic |
| 109 | + num = 1000 |
| 110 | + arr = np.array(['a'] * num + ['b'] * num + ['c'] * num, |
| 111 | + dtype=self.dtype) |
| 112 | + engine = self.engine_type(lambda: arr, len(arr)) |
| 113 | + assert engine.get_loc('b') == slice(1000, 2000) |
| 114 | + |
| 115 | + # not monotonic |
| 116 | + arr = np.array(['a', 'b', 'c'] * num, dtype=self.dtype) |
| 117 | + engine = self.engine_type(lambda: arr, len(arr)) |
| 118 | + expected = np.array([False, True, False] * num, dtype=bool) |
| 119 | + result = engine.get_loc('b') |
| 120 | + assert (result == expected).all() |
0 commit comments