|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +import pandas.util.testing as tm |
| 4 | +from pandas import compat |
| 5 | +from pandas._libs import algos as libalgos, index as libindex |
| 6 | + |
| 7 | + |
| 8 | +class TestNumericEngine(object): |
| 9 | + def test_is_monotonic(self, numeric_indexing_engine_type_and_dtype): |
| 10 | + engine_type, dtype = numeric_indexing_engine_type_and_dtype |
| 11 | + num = 1000 |
| 12 | + arr = np.array([1] * num + [2] * num + [3] * num, dtype=dtype) |
| 13 | + |
| 14 | + # monotonic increasing |
| 15 | + engine = engine_type(lambda: arr, len(arr)) |
| 16 | + assert engine.is_monotonic_increasing is True |
| 17 | + assert engine.is_monotonic_decreasing is False |
| 18 | + |
| 19 | + # monotonic decreasing |
| 20 | + engine = engine_type(lambda: arr[::-1], len(arr)) |
| 21 | + assert engine.is_monotonic_increasing is False |
| 22 | + assert engine.is_monotonic_decreasing is True |
| 23 | + |
| 24 | + # neither monotonic increasing or decreasing |
| 25 | + arr = np.array([1] * num + [2] * num + [1] * num, dtype=dtype) |
| 26 | + engine = engine_type(lambda: arr[::-1], len(arr)) |
| 27 | + assert engine.is_monotonic_increasing is False |
| 28 | + assert engine.is_monotonic_decreasing is False |
| 29 | + |
| 30 | + def test_is_unique(self, numeric_indexing_engine_type_and_dtype): |
| 31 | + engine_type, dtype = numeric_indexing_engine_type_and_dtype |
| 32 | + |
| 33 | + # unique |
| 34 | + arr = np.array([1, 3, 2], dtype=dtype) |
| 35 | + engine = engine_type(lambda: arr, len(arr)) |
| 36 | + assert engine.is_unique is True |
| 37 | + |
| 38 | + # not unique |
| 39 | + arr = np.array([1, 2, 1], dtype=dtype) |
| 40 | + engine = engine_type(lambda: arr, len(arr)) |
| 41 | + assert engine.is_unique is False |
| 42 | + |
| 43 | + def test_get_loc(self, numeric_indexing_engine_type_and_dtype): |
| 44 | + engine_type, dtype = numeric_indexing_engine_type_and_dtype |
| 45 | + |
| 46 | + # unique |
| 47 | + arr = np.array([1, 2, 3], dtype=dtype) |
| 48 | + engine = engine_type(lambda: arr, len(arr)) |
| 49 | + assert engine.get_loc(2) == 1 |
| 50 | + |
| 51 | + # monotonic |
| 52 | + num = 1000 |
| 53 | + arr = np.array([1] * num + [2] * num + [3] * num, dtype=dtype) |
| 54 | + engine = engine_type(lambda: arr, len(arr)) |
| 55 | + assert engine.get_loc(2) == slice(1000, 2000) |
| 56 | + |
| 57 | + # not monotonic |
| 58 | + arr = np.array([1, 2, 3] * num, dtype=dtype) |
| 59 | + engine = engine_type(lambda: arr, len(arr)) |
| 60 | + expected = np.array([False, True, False] * num, dtype=bool) |
| 61 | + result = engine.get_loc(2) |
| 62 | + assert (result == expected).all() |
| 63 | + |
| 64 | + def test_get_backfill_indexer( |
| 65 | + self, numeric_indexing_engine_type_and_dtype): |
| 66 | + engine_type, dtype = numeric_indexing_engine_type_and_dtype |
| 67 | + |
| 68 | + arr = np.array([1, 5, 10], dtype=dtype) |
| 69 | + engine = engine_type(lambda: arr, len(arr)) |
| 70 | + |
| 71 | + new = np.array(compat.range(12), dtype=dtype) |
| 72 | + result = engine.get_backfill_indexer(new) |
| 73 | + |
| 74 | + expected = libalgos.backfill(arr, new) |
| 75 | + tm.assert_numpy_array_equal(result, expected) |
| 76 | + |
| 77 | + def test_get_pad_indexer( |
| 78 | + self, numeric_indexing_engine_type_and_dtype): |
| 79 | + engine_type, dtype = numeric_indexing_engine_type_and_dtype |
| 80 | + |
| 81 | + arr = np.array([1, 5, 10], dtype=dtype) |
| 82 | + engine = engine_type(lambda: arr, len(arr)) |
| 83 | + |
| 84 | + new = np.array(compat.range(12), dtype=dtype) |
| 85 | + result = engine.get_pad_indexer(new) |
| 86 | + |
| 87 | + expected = libalgos.pad(arr, new) |
| 88 | + tm.assert_numpy_array_equal(result, expected) |
| 89 | + |
| 90 | + |
| 91 | +class TestObjectEngine(object): |
| 92 | + engine_type = libindex.ObjectEngine |
| 93 | + dtype = np.object_ |
| 94 | + values = list('abc') |
| 95 | + |
| 96 | + def test_is_monotonic(self): |
| 97 | + |
| 98 | + num = 1000 |
| 99 | + arr = np.array(['a'] * num + ['a'] * num + ['c'] * num, |
| 100 | + dtype=self.dtype) |
| 101 | + |
| 102 | + # monotonic increasing |
| 103 | + engine = self.engine_type(lambda: arr, len(arr)) |
| 104 | + assert engine.is_monotonic_increasing is True |
| 105 | + assert engine.is_monotonic_decreasing is False |
| 106 | + |
| 107 | + # monotonic decreasing |
| 108 | + engine = self.engine_type(lambda: arr[::-1], len(arr)) |
| 109 | + assert engine.is_monotonic_increasing is False |
| 110 | + assert engine.is_monotonic_decreasing is True |
| 111 | + |
| 112 | + # neither monotonic increasing or decreasing |
| 113 | + arr = np.array(['a'] * num + ['b'] * num + ['a'] * num, |
| 114 | + dtype=self.dtype) |
| 115 | + engine = self.engine_type(lambda: arr[::-1], len(arr)) |
| 116 | + assert engine.is_monotonic_increasing is False |
| 117 | + assert engine.is_monotonic_decreasing is False |
| 118 | + |
| 119 | + def test_is_unique(self): |
| 120 | + # unique |
| 121 | + arr = np.array(self.values, dtype=self.dtype) |
| 122 | + engine = self.engine_type(lambda: arr, len(arr)) |
| 123 | + assert engine.is_unique is True |
| 124 | + |
| 125 | + # not unique |
| 126 | + arr = np.array(['a', 'b', 'a'], dtype=self.dtype) |
| 127 | + engine = self.engine_type(lambda: arr, len(arr)) |
| 128 | + assert engine.is_unique is False |
| 129 | + |
| 130 | + def test_get_loc(self): |
| 131 | + # unique |
| 132 | + arr = np.array(self.values, dtype=self.dtype) |
| 133 | + engine = self.engine_type(lambda: arr, len(arr)) |
| 134 | + assert engine.get_loc('b') == 1 |
| 135 | + |
| 136 | + # monotonic |
| 137 | + num = 1000 |
| 138 | + arr = np.array(['a'] * num + ['b'] * num + ['c'] * num, |
| 139 | + dtype=self.dtype) |
| 140 | + engine = self.engine_type(lambda: arr, len(arr)) |
| 141 | + assert engine.get_loc('b') == slice(1000, 2000) |
| 142 | + |
| 143 | + # not monotonic |
| 144 | + arr = np.array(self.values * num, dtype=self.dtype) |
| 145 | + engine = self.engine_type(lambda: arr, len(arr)) |
| 146 | + expected = np.array([False, True, False] * num, dtype=bool) |
| 147 | + result = engine.get_loc('b') |
| 148 | + assert (result == expected).all() |
| 149 | + |
| 150 | + def test_get_backfill_indexer(self): |
| 151 | + arr = np.array(['a', 'e', 'j'], dtype=self.dtype) |
| 152 | + engine = self.engine_type(lambda: arr, len(arr)) |
| 153 | + |
| 154 | + new = np.array(list('abcdefghij'), dtype=self.dtype) |
| 155 | + result = engine.get_backfill_indexer(new) |
| 156 | + |
| 157 | + expected = libalgos.backfill_object(arr, new) |
| 158 | + tm.assert_numpy_array_equal(result, expected) |
| 159 | + |
| 160 | + def test_get_pad_indexer(self): |
| 161 | + arr = np.array(['a', 'e', 'j'], dtype=self.dtype) |
| 162 | + engine = self.engine_type(lambda: arr, len(arr)) |
| 163 | + |
| 164 | + new = np.array(list('abcdefghij'), dtype=self.dtype) |
| 165 | + result = engine.get_pad_indexer(new) |
| 166 | + |
| 167 | + expected = libalgos.pad_object(arr, new) |
| 168 | + tm.assert_numpy_array_equal(result, expected) |
0 commit comments