diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index b8a9827b5effd..1604139b36848 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -4101,37 +4101,13 @@ def append(self, other): return self._concat(to_concat, name) def _concat(self, to_concat, name): - - typs = _concat.get_dtype_kinds(to_concat) - - if len(typs) == 1: - return self._concat_same_dtype(to_concat, name=name) - return Index._concat_same_dtype(self, to_concat, name=name) - - def _concat_same_dtype(self, to_concat, name): """ - Concatenate to_concat which has the same class. + Concatenate multiple Index objects. """ - # must be overridden in specific classes - klasses = ( - ABCDatetimeIndex, - ABCTimedeltaIndex, - ABCPeriodIndex, - ExtensionArray, - ABCIntervalIndex, - ) - to_concat = [ - x.astype(object) if isinstance(x, klasses) else x for x in to_concat - ] - - self = to_concat[0] - attribs = self._get_attributes_dict() - attribs["name"] = name - to_concat = [x._values if isinstance(x, Index) else x for x in to_concat] - res_values = np.concatenate(to_concat) - return Index(res_values, name=name) + result = _concat.concat_compat(to_concat) + return Index(result, name=name) def putmask(self, mask, value): """ diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index 25df4a0bee737..2a79c83de7ef2 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -738,13 +738,6 @@ def insert(self, loc: int, item): def _concat(self, to_concat, name): # if calling index is category, don't check dtype of others - return CategoricalIndex._concat_same_dtype(self, to_concat, name) - - def _concat_same_dtype(self, to_concat, name): - """ - Concatenate to_concat which has the same class - ValueError if other is not in the categories - """ codes = np.concatenate([self._is_dtype_compat(c).codes for c in to_concat]) result = self._create_from_codes(codes, name=name) # if name is None, _create_from_codes sets self.name diff --git a/pandas/core/indexes/extension.py b/pandas/core/indexes/extension.py index 664e49313507f..badf6502aa723 100644 --- a/pandas/core/indexes/extension.py +++ b/pandas/core/indexes/extension.py @@ -236,10 +236,6 @@ def insert(self, loc: int, item): # ExtensionIndex subclasses must override Index.insert raise AbstractMethodError(self) - def _concat_same_dtype(self, to_concat, name): - arr = type(self._data)._concat_same_type(to_concat) - return type(self)._simple_new(arr, name=name) - def _get_unique_index(self, dropna=False): if self.is_unique and not dropna: return self diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index 06040166d0f9e..5020a25c88ff4 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -147,10 +147,6 @@ def _assert_safe_casting(cls, data, subarr): """ pass - def _concat_same_dtype(self, indexes, name): - result = type(indexes[0])(np.concatenate([x._values for x in indexes])) - return result.rename(name) - @property def is_all_dates(self) -> bool: """ diff --git a/pandas/core/indexes/range.py b/pandas/core/indexes/range.py index c34b8965ca36a..49a0f0fb7ae92 100644 --- a/pandas/core/indexes/range.py +++ b/pandas/core/indexes/range.py @@ -627,14 +627,18 @@ def join(self, other, how="left", level=None, return_indexers=False, sort=False) return super().join(other, how, level, return_indexers, sort) - def _concat_same_dtype(self, indexes, name): + def _concat(self, indexes, name): """ - Concatenates multiple RangeIndex instances. All members of "indexes" must - be of type RangeIndex; result will be RangeIndex if possible, Int64Index - otherwise. E.g.: + Overriding parent method for the case of all RangeIndex instances. + + When all members of "indexes" are of type RangeIndex: result will be + RangeIndex if possible, Int64Index otherwise. E.g.: indexes = [RangeIndex(3), RangeIndex(3, 6)] -> RangeIndex(6) indexes = [RangeIndex(3), RangeIndex(4, 6)] -> Int64Index([0,1,2,4,5]) """ + if not all(isinstance(x, RangeIndex) for x in indexes): + return super()._concat(indexes, name) + start = step = next_ = None # Filter the empty indexes diff --git a/pandas/tests/arrays/test_datetimelike.py b/pandas/tests/arrays/test_datetimelike.py index 61d78034f0747..d0bf5bb41bb2c 100644 --- a/pandas/tests/arrays/test_datetimelike.py +++ b/pandas/tests/arrays/test_datetimelike.py @@ -168,7 +168,8 @@ def test_concat_same_type(self): arr = self.array_cls(idx) result = arr._concat_same_type([arr[:-1], arr[1:], arr]) - expected = idx._concat_same_dtype([idx[:-1], idx[1:], idx], None) + arr2 = arr.astype(object) + expected = self.index_cls(np.concatenate([arr2[:-1], arr2[1:], arr2]), None) tm.assert_index_equal(self.index_cls(result), expected) diff --git a/pandas/tests/indexes/categorical/test_category.py b/pandas/tests/indexes/categorical/test_category.py index 9765c77c6b60c..8a84090ea6e94 100644 --- a/pandas/tests/indexes/categorical/test_category.py +++ b/pandas/tests/indexes/categorical/test_category.py @@ -136,7 +136,7 @@ def test_append(self): tm.assert_index_equal(result, expected, exact=True) def test_append_to_another(self): - # hits Index._concat_same_dtype + # hits Index._concat fst = Index(["a", "b"]) snd = CategoricalIndex(["d", "e"]) result = fst.append(snd)