11import numpy as np
22import pytest
33
4+ from pandas .errors import InvalidIndexError
5+
46import pandas as pd
57from pandas import CategoricalIndex , Index , IntervalIndex , Timestamp
68import pandas ._testing as tm
@@ -204,18 +206,19 @@ def test_get_indexer_base(self):
204206 with pytest .raises (ValueError , match = "Invalid fill method" ):
205207 idx .get_indexer (idx , method = "invalid" )
206208
207- def test_get_indexer_non_unique (self ):
209+ def test_get_indexer_requires_unique (self ):
208210 np .random .seed (123456789 )
209211
210212 ci = CategoricalIndex (list ("aabbca" ), categories = list ("cab" ), ordered = False )
211213 oidx = Index (np .array (ci ))
212214
215+ msg = "Reindexing only valid with uniquely valued Index objects"
216+
213217 for n in [1 , 2 , 5 , len (ci )]:
214218 finder = oidx [np .random .randint (0 , len (ci ), size = n )]
215- expected = oidx .get_indexer_non_unique (finder )[0 ]
216219
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 )
219222
220223 # see gh-17323
221224 #
@@ -224,19 +227,27 @@ def test_get_indexer_non_unique(self):
224227 # respect duplicates instead of taking
225228 # the fast-track path.
226229 for finder in [list ("aabbca" ), list ("aababca" )]:
227- expected = oidx .get_indexer_non_unique (finder )[0 ]
228230
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 )
231233
232- def test_get_indexer (self ):
234+ def test_get_indexer_non_unique (self ):
233235
234236 idx1 = CategoricalIndex (list ("aabcde" ), categories = list ("edabc" ))
235237 idx2 = CategoricalIndex (list ("abf" ))
236238
237239 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" ))
240251
241252 msg = "method pad not yet implemented for CategoricalIndex"
242253 with pytest .raises (NotImplementedError , match = msg ):
0 commit comments