diff --git a/pandas/_libs/index.pyx b/pandas/_libs/index.pyx index 18e0bd4014ac8..6186ad3388ace 100644 --- a/pandas/_libs/index.pyx +++ b/pandas/_libs/index.pyx @@ -360,6 +360,8 @@ cdef class IndexEngine: values = self.values stargets = set(targets) + na_in_stargets = any([checknull(t) for t in stargets]) + n = len(values) n_t = len(targets) if n > 10_000: @@ -374,7 +376,7 @@ cdef class IndexEngine: if ( stargets and len(stargets) < 5 and - not any([checknull(t) for t in stargets]) and + not na_in_stargets and self.is_monotonic_increasing ): # if there are few enough stargets and the index is monotonically @@ -396,7 +398,7 @@ cdef class IndexEngine: # otherwise, map by iterating through all items in the index # short-circuit na check - if values.dtype == object: + if na_in_stargets: check_na_values = True # keep track of nas in values found_nas = set() diff --git a/pandas/tests/indexes/base_class/test_indexing.py b/pandas/tests/indexes/base_class/test_indexing.py index 070fec47b90e6..32fce04824b40 100644 --- a/pandas/tests/indexes/base_class/test_indexing.py +++ b/pandas/tests/indexes/base_class/test_indexing.py @@ -37,6 +37,32 @@ def test_get_indexer_non_unique_dtype_mismatch(self): tm.assert_numpy_array_equal(np.array([-1], dtype=np.intp), indexes) tm.assert_numpy_array_equal(np.array([0], dtype=np.intp), missing) + def test_get_indexer_non_unique_int_index(self): + indexes, missing = Index([np.nan, 100, 200, 100]).get_indexer_non_unique( + Index([np.nan]) + ) + tm.assert_numpy_array_equal(np.array([0], dtype=np.intp), indexes) + tm.assert_numpy_array_equal(np.array([], dtype=np.intp), missing) + + indexes, missing = Index([np.nan, 100, 200, 100]).get_indexer_non_unique( + Index([np.nan, 100]) + ) + tm.assert_numpy_array_equal(np.array([0, 1, 3], dtype=np.intp), indexes) + tm.assert_numpy_array_equal(np.array([], dtype=np.intp), missing) + + def test_get_indexer_non_unique_float_index(self): + indexes, missing = Index([np.nan, 100.0, 200.0, 100.0]).get_indexer_non_unique( + Index([np.nan]) + ) + tm.assert_numpy_array_equal(np.array([0], dtype=np.intp), indexes) + tm.assert_numpy_array_equal(np.array([], dtype=np.intp), missing) + + indexes, missing = Index([np.nan, 100.0, 200.0, 100.0]).get_indexer_non_unique( + Index([np.nan, 100.0]) + ) + tm.assert_numpy_array_equal(np.array([0, 1, 3], dtype=np.intp), indexes) + tm.assert_numpy_array_equal(np.array([], dtype=np.intp), missing) + class TestGetLoc: @pytest.mark.slow # to_flat_index takes a while