diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 6219aa07478d7..7b58e34152547 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3077,7 +3077,7 @@ def intersection(self, other, sort=False): return Index([], name=result_name) elif not is_dtype_equal(self.dtype, other.dtype): - dtype = find_common_type([self.dtype, other.dtype]) + dtype = self._find_common_type_compat(other) this = self.astype(dtype, copy=False) other = other.astype(dtype, copy=False) return this.intersection(other, sort=sort) @@ -3429,6 +3429,10 @@ def get_indexer( ptarget, method=method, limit=limit, tolerance=tolerance ) + if is_dtype_equal(self.dtype, target.dtype) and self.equals(target): + # Only call equals if we have same dtype to avoid inference/casting + return np.arange(len(target), dtype=np.intp) + return self._get_indexer(target, method, limit, tolerance) def _get_indexer( @@ -3937,8 +3941,9 @@ def join( return join_index, lidx, ridx if not is_dtype_equal(self.dtype, other.dtype): - this = self.astype("O") - other = other.astype("O") + dtype = self._find_common_type_compat(other) + this = self.astype(dtype, copy=False) + other = other.astype(dtype, copy=False) return this.join(other, how=how, return_indexers=True) _validate_join_method(how) @@ -5216,6 +5221,8 @@ def get_indexer_non_unique(self, target) -> tuple[np.ndarray, np.ndarray]: that = target.astype(dtype, copy=False) return this.get_indexer_non_unique(that) + # Note: _maybe_promote ensures we never get here with MultiIndex + # self and non-Multi target tgt_values = target._get_engine_target() indexer, missing = self._engine.get_indexer_non_unique(tgt_values) @@ -5948,8 +5955,7 @@ def insert(self, loc: int, item) -> Index: try: item = self._validate_fill_value(item) except TypeError: - inferred, _ = infer_dtype_from(item) - dtype = find_common_type([self.dtype, inferred]) + dtype = self._find_common_type_compat(item) return self.astype(dtype).insert(loc, item) arr = np.asarray(self) diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index e71d6d690bcda..e6764dd01e6d3 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -646,10 +646,6 @@ def _get_indexer( # returned ndarray is np.intp if isinstance(target, IntervalIndex): - # equal indexes -> 1:1 positional match - if self.equals(target): - return np.arange(len(self), dtype="intp") - if not self._should_compare(target): return self._get_indexer_non_comparable(target, method, unique=True) diff --git a/pandas/tests/libs/test_hashtable.py b/pandas/tests/libs/test_hashtable.py index 08bfc74e0ef8d..5ff20051da8c0 100644 --- a/pandas/tests/libs/test_hashtable.py +++ b/pandas/tests/libs/test_hashtable.py @@ -252,9 +252,9 @@ def test_get_labels_groupby_for_Int64(writable): vals = np.array([1, 2, -1, 2, 1, -1], dtype=np.int64) vals.flags.writeable = writable arr, unique = table.get_labels_groupby(vals) - expected_arr = np.array([0, 1, -1, 1, 0, -1], dtype=np.int64) + expected_arr = np.array([0, 1, -1, 1, 0, -1], dtype=np.intp) expected_unique = np.array([1, 2], dtype=np.int64) - tm.assert_numpy_array_equal(arr.astype(np.int64), expected_arr) + tm.assert_numpy_array_equal(arr, expected_arr) tm.assert_numpy_array_equal(unique, expected_unique)