1
1
import numpy as np
2
2
import pytest
3
3
4
+ from pandas .errors import InvalidIndexError
5
+
4
6
import pandas as pd
5
7
from pandas import CategoricalIndex , Index , IntervalIndex , Timestamp
6
8
import pandas ._testing as tm
@@ -204,18 +206,19 @@ def test_get_indexer_base(self):
204
206
with pytest .raises (ValueError , match = "Invalid fill method" ):
205
207
idx .get_indexer (idx , method = "invalid" )
206
208
207
- def test_get_indexer_non_unique (self ):
209
+ def test_get_indexer_requires_unique (self ):
208
210
np .random .seed (123456789 )
209
211
210
212
ci = CategoricalIndex (list ("aabbca" ), categories = list ("cab" ), ordered = False )
211
213
oidx = Index (np .array (ci ))
212
214
215
+ msg = "Reindexing only valid with uniquely valued Index objects"
216
+
213
217
for n in [1 , 2 , 5 , len (ci )]:
214
218
finder = oidx [np .random .randint (0 , len (ci ), size = n )]
215
- expected = oidx .get_indexer_non_unique (finder )[0 ]
216
219
217
- actual = ci . get_indexer ( finder )
218
- tm . assert_numpy_array_equal ( expected , actual )
220
+ with pytest . raises ( InvalidIndexError , match = msg ):
221
+ ci . get_indexer ( finder )
219
222
220
223
# see gh-17323
221
224
#
@@ -224,19 +227,27 @@ def test_get_indexer_non_unique(self):
224
227
# respect duplicates instead of taking
225
228
# the fast-track path.
226
229
for finder in [list ("aabbca" ), list ("aababca" )]:
227
- expected = oidx .get_indexer_non_unique (finder )[0 ]
228
230
229
- actual = ci . get_indexer ( finder )
230
- tm . assert_numpy_array_equal ( expected , actual )
231
+ with pytest . raises ( InvalidIndexError , match = msg ):
232
+ ci . get_indexer ( finder )
231
233
232
- def test_get_indexer (self ):
234
+ def test_get_indexer_non_unique (self ):
233
235
234
236
idx1 = CategoricalIndex (list ("aabcde" ), categories = list ("edabc" ))
235
237
idx2 = CategoricalIndex (list ("abf" ))
236
238
237
239
for indexer in [idx2 , list ("abf" ), Index (list ("abf" ))]:
238
- r1 = idx1 .get_indexer (idx2 )
239
- tm .assert_almost_equal (r1 , np .array ([0 , 1 , 2 , - 1 ], dtype = np .intp ))
240
+ msg = "Reindexing only valid with uniquely valued Index objects"
241
+ with pytest .raises (InvalidIndexError , match = msg ):
242
+ idx1 .get_indexer (idx2 )
243
+
244
+ r1 , _ = idx1 .get_indexer_non_unique (idx2 )
245
+ expected = np .array ([0 , 1 , 2 , - 1 ], dtype = np .intp )
246
+ tm .assert_almost_equal (r1 , expected )
247
+
248
+ def test_get_indexer_method (self ):
249
+ idx1 = CategoricalIndex (list ("aabcde" ), categories = list ("edabc" ))
250
+ idx2 = CategoricalIndex (list ("abf" ))
240
251
241
252
msg = "method pad not yet implemented for CategoricalIndex"
242
253
with pytest .raises (NotImplementedError , match = msg ):
0 commit comments