|
12 | 12 |
|
13 | 13 | class TestIntervalIndex(object):
|
14 | 14 |
|
15 |
| - def _compare_tuple_of_numpy_array(self, result, expected): |
16 |
| - lidx, ridx = result |
17 |
| - lidx_expected, ridx_expected = expected |
18 |
| - |
19 |
| - tm.assert_numpy_array_equal(lidx, lidx_expected) |
20 |
| - tm.assert_numpy_array_equal(ridx, ridx_expected) |
21 |
| - |
22 | 15 | @pytest.mark.parametrize("side", ['right', 'left', 'both', 'neither'])
|
23 | 16 | def test_get_loc_interval(self, closed, side):
|
24 | 17 |
|
@@ -149,107 +142,94 @@ def test_slice_locs_with_ints_and_floats_errors(self, tuples, query):
|
149 | 142 | index.slice_locs(query)
|
150 | 143 |
|
151 | 144 | @pytest.mark.parametrize('query, expected', [
|
152 |
| - (Interval(1, 3, closed='right'), 1), |
153 |
| - (Interval(1, 3, closed='left'), -1), |
154 |
| - (Interval(1, 3, closed='both'), -1), |
155 |
| - (Interval(1, 3, closed='neither'), -1), |
156 |
| - (Interval(1, 4, closed='right'), -1), |
157 |
| - (Interval(0, 4, closed='right'), -1), |
158 |
| - (Interval(1, 2, closed='right'), -1)]) |
159 |
| - def test_get_indexer_with_interval_single_queries(self, query, expected): |
160 |
| - |
161 |
| - index = IntervalIndex.from_tuples( |
162 |
| - [(0, 2.5), (1, 3), (2, 4)], closed='right') |
163 |
| - |
164 |
| - result = index.get_indexer([query]) |
165 |
| - expected = np.array([expected], dtype='intp') |
166 |
| - tm.assert_numpy_array_equal(result, expected) |
167 |
| - |
168 |
| - @pytest.mark.parametrize('query, expected', [ |
| 145 | + ([Interval(1, 3, closed='right')], [1]), |
| 146 | + ([Interval(1, 3, closed='left')], [-1]), |
| 147 | + ([Interval(1, 3, closed='both')], [-1]), |
| 148 | + ([Interval(1, 3, closed='neither')], [-1]), |
| 149 | + ([Interval(1, 4, closed='right')], [-1]), |
| 150 | + ([Interval(0, 4, closed='right')], [-1]), |
| 151 | + ([Interval(1, 2, closed='right')], [-1]), |
169 | 152 | ([Interval(2, 4, closed='right'), Interval(1, 3, closed='right')],
|
170 | 153 | [2, 1]),
|
171 | 154 | ([Interval(1, 3, closed='right'), Interval(0, 2, closed='right')],
|
172 | 155 | [1, -1]),
|
173 | 156 | ([Interval(1, 3, closed='right'), Interval(1, 3, closed='left')],
|
174 | 157 | [1, -1])])
|
175 |
| - def test_get_indexer_with_interval_multiple_queries(self, query, expected): |
| 158 | + def test_get_indexer_with_interval(self, query, expected): |
176 | 159 |
|
177 |
| - index = IntervalIndex.from_tuples( |
178 |
| - [(0, 2.5), (1, 3), (2, 4)], closed='right') |
| 160 | + tuples = [(0, 2.5), (1, 3), (2, 4)] |
| 161 | + index = IntervalIndex.from_tuples(tuples, closed='right') |
179 | 162 |
|
180 | 163 | result = index.get_indexer(query)
|
181 | 164 | expected = np.array(expected, dtype='intp')
|
182 | 165 | tm.assert_numpy_array_equal(result, expected)
|
183 | 166 |
|
184 | 167 | @pytest.mark.parametrize('query, expected', [
|
185 |
| - (-0.5, -1), (0, -1), (0.5, 0), (1, 0), (1.5, 1), (2, 1), |
186 |
| - (2.5, -1), (3, -1), (3.5, 2), (4, 2), (4.5, -1)]) |
187 |
| - def test_get_indexer_with_ints_and_floats_single_queries( |
188 |
| - self, query, expected): |
189 |
| - |
190 |
| - index = IntervalIndex.from_tuples( |
191 |
| - [(0, 1), (1, 2), (3, 4)], closed='right') |
192 |
| - |
193 |
| - result = index.get_indexer([query]) |
194 |
| - expected = np.array([expected], dtype='intp') |
195 |
| - tm.assert_numpy_array_equal(result, expected) |
196 |
| - |
197 |
| - @pytest.mark.parametrize('query, expected', [ |
| 168 | + ([-0.5], [-1]), |
| 169 | + ([0], [-1]), |
| 170 | + ([0.5], [0]), |
| 171 | + ([1], [0]), |
| 172 | + ([1.5], [1]), |
| 173 | + ([2], [1]), |
| 174 | + ([2.5], [-1]), |
| 175 | + ([3], [-1]), |
| 176 | + ([3.5], [2]), |
| 177 | + ([4], [2]), |
| 178 | + ([4.5], [-1]), |
198 | 179 | ([1, 2], [0, 1]),
|
199 | 180 | ([1, 2, 3], [0, 1, -1]),
|
200 | 181 | ([1, 2, 3, 4], [0, 1, -1, 2]),
|
201 | 182 | ([1, 2, 3, 4, 2], [0, 1, -1, 2, 1])])
|
202 |
| - def test_get_indexer_with_ints_and_floats_multiple_queries( |
203 |
| - self, query, expected): |
| 183 | + def test_get_indexer_with_int_and_float(self, query, expected): |
204 | 184 |
|
205 |
| - index = IntervalIndex.from_tuples( |
206 |
| - [(0, 1), (1, 2), (3, 4)], closed='right') |
| 185 | + tuples = [(0, 1), (1, 2), (3, 4)] |
| 186 | + index = IntervalIndex.from_tuples(tuples, closed='right') |
207 | 187 |
|
208 | 188 | result = index.get_indexer(query)
|
209 | 189 | expected = np.array(expected, dtype='intp')
|
210 | 190 | tm.assert_numpy_array_equal(result, expected)
|
211 | 191 |
|
212 |
| - index = IntervalIndex.from_tuples([(0, 2), (1, 3), (2, 4)]) |
213 |
| - # TODO: @shoyer believes this should raise, master branch doesn't |
| 192 | + @pytest.mark.parametrize('tuples, closed', [ |
| 193 | + ([(0, 2), (1, 3), (3, 4)], 'neither'), |
| 194 | + ([(0, 5), (1, 4), (6, 7)], 'left'), |
| 195 | + ([(0, 1), (0, 1), (1, 2)], 'right'), |
| 196 | + ([(0, 1), (2, 3), (3, 4)], 'both')]) |
| 197 | + def test_get_indexer_errors(self, tuples, closed): |
| 198 | + # IntervalIndex needs non-overlapping for uniqueness when querying |
| 199 | + index = IntervalIndex.from_tuples(tuples, closed=closed) |
214 | 200 |
|
215 |
| - @pytest.mark.parametrize('query, expected', [ |
216 |
| - (-0.5, (Int64Index([], dtype='int64'), np.array([0]))), |
217 |
| - (0, (Int64Index([0], dtype='int64'), np.array([]))), |
218 |
| - (0.5, (Int64Index([0], dtype='int64'), np.array([]))), |
219 |
| - (1, (Int64Index([0, 1], dtype='int64'), np.array([]))), |
220 |
| - (1.5, (Int64Index([0, 1], dtype='int64'), np.array([]))), |
221 |
| - (2, (Int64Index([0, 1, 2], dtype='int64'), np.array([]))), |
222 |
| - (2.5, (Int64Index([1, 2], dtype='int64'), np.array([]))), |
223 |
| - (3, (Int64Index([2], dtype='int64'), np.array([]))), |
224 |
| - (3.5, (Int64Index([2], dtype='int64'), np.array([]))), |
225 |
| - (4, (Int64Index([], dtype='int64'), np.array([0]))), |
226 |
| - (4.5, (Int64Index([], dtype='int64'), np.array([0])))]) |
227 |
| - def test_get_indexer_non_unique_with_ints_and_floats_single_queries( |
228 |
| - self, query, expected): |
229 |
| - |
230 |
| - index = IntervalIndex.from_tuples( |
231 |
| - [(0, 2.5), (1, 3), (2, 4)], closed='left') |
232 |
| - |
233 |
| - result = index.get_indexer_non_unique([query]) |
234 |
| - tm.assert_numpy_array_equal(result, expected) |
| 201 | + msg = ('cannot handle overlapping indices; use ' |
| 202 | + 'IntervalIndex.get_indexer_non_unique') |
| 203 | + with tm.assert_raises_regex(ValueError, msg): |
| 204 | + index.get_indexer([0, 2]) |
235 | 205 |
|
236 | 206 | @pytest.mark.parametrize('query, expected', [
|
237 |
| - ([1, 2], (Int64Index([0, 1, 0, 1, 2], dtype='int64'), np.array([]))), |
238 |
| - ([1, 2, 3], |
239 |
| - (Int64Index([0, 1, 0, 1, 2, 2], dtype='int64'), np.array([]))), |
240 |
| - ([1, 2, 3, 4], |
241 |
| - (Int64Index([0, 1, 0, 1, 2, 2, -1], dtype='int64'), np.array([3]))), |
242 |
| - ([1, 2, 3, 4, 2], |
243 |
| - (Int64Index([0, 1, 0, 1, 2, 2, -1, 0, 1, 2], dtype='int64'), |
244 |
| - np.array([3])))]) |
245 |
| - def test_get_indexer_non_unique_with_ints_and_floats_multiple_queries( |
246 |
| - self, query, expected): |
247 |
| - |
248 |
| - index = IntervalIndex.from_tuples( |
249 |
| - [(0, 2.5), (1, 3), (2, 4)], closed='left') |
250 |
| - |
251 |
| - result = index.get_indexer_non_unique(query) |
252 |
| - tm.assert_numpy_array_equal(result, expected) |
| 207 | + ([-0.5], ([-1], [0])), |
| 208 | + ([0], ([0], [])), |
| 209 | + ([0.5], ([0], [])), |
| 210 | + ([1], ([0, 1], [])), |
| 211 | + ([1.5], ([0, 1], [])), |
| 212 | + ([2], ([0, 1, 2], [])), |
| 213 | + ([2.5], ([1, 2], [])), |
| 214 | + ([3], ([2], [])), |
| 215 | + ([3.5], ([2], [])), |
| 216 | + ([4], ([-1], [0])), |
| 217 | + ([4.5], ([-1], [0])), |
| 218 | + ([1, 2], ([0, 1, 0, 1, 2], [])), |
| 219 | + ([1, 2, 3], ([0, 1, 0, 1, 2, 2], [])), |
| 220 | + ([1, 2, 3, 4], ([0, 1, 0, 1, 2, 2, -1], [3])), |
| 221 | + ([1, 2, 3, 4, 2], ([0, 1, 0, 1, 2, 2, -1, 0, 1, 2], [3]))]) |
| 222 | + def test_get_indexer_non_unique_with_int_and_float(self, query, expected): |
| 223 | + |
| 224 | + tuples = [(0, 2.5), (1, 3), (2, 4)] |
| 225 | + index = IntervalIndex.from_tuples(tuples, closed='left') |
| 226 | + |
| 227 | + result_indexer, result_missing = index.get_indexer_non_unique(query) |
| 228 | + expected_indexer = Int64Index(expected[0]) |
| 229 | + expected_missing = np.array(expected[1], dtype='intp') |
| 230 | + |
| 231 | + tm.assert_index_equal(result_indexer, expected_indexer) |
| 232 | + tm.assert_numpy_array_equal(result_missing, expected_missing) |
253 | 233 |
|
254 | 234 | # TODO we may also want to test get_indexer for the case when
|
255 | 235 | # the intervals are duplicated, decreasing, non-monotonic, etc..
|
|
0 commit comments