Skip to content

Commit 63133a4

Browse files
CLN: let Index use general concat machinery - remove Index._concat_same_dtype (#34198)
1 parent 0f52751 commit 63133a4

File tree

7 files changed

+14
-48
lines changed

7 files changed

+14
-48
lines changed

pandas/core/indexes/base.py

+3-27
Original file line numberDiff line numberDiff line change
@@ -4102,37 +4102,13 @@ def append(self, other):
41024102
return self._concat(to_concat, name)
41034103

41044104
def _concat(self, to_concat, name):
4105-
4106-
typs = _concat.get_dtype_kinds(to_concat)
4107-
4108-
if len(typs) == 1:
4109-
return self._concat_same_dtype(to_concat, name=name)
4110-
return Index._concat_same_dtype(self, to_concat, name=name)
4111-
4112-
def _concat_same_dtype(self, to_concat, name):
41134105
"""
4114-
Concatenate to_concat which has the same class.
4106+
Concatenate multiple Index objects.
41154107
"""
4116-
# must be overridden in specific classes
4117-
klasses = (
4118-
ABCDatetimeIndex,
4119-
ABCTimedeltaIndex,
4120-
ABCPeriodIndex,
4121-
ExtensionArray,
4122-
ABCIntervalIndex,
4123-
)
4124-
to_concat = [
4125-
x.astype(object) if isinstance(x, klasses) else x for x in to_concat
4126-
]
4127-
4128-
self = to_concat[0]
4129-
attribs = self._get_attributes_dict()
4130-
attribs["name"] = name
4131-
41324108
to_concat = [x._values if isinstance(x, Index) else x for x in to_concat]
41334109

4134-
res_values = np.concatenate(to_concat)
4135-
return Index(res_values, name=name)
4110+
result = _concat.concat_compat(to_concat)
4111+
return Index(result, name=name)
41364112

41374113
def putmask(self, mask, value):
41384114
"""

pandas/core/indexes/category.py

-7
Original file line numberDiff line numberDiff line change
@@ -738,13 +738,6 @@ def insert(self, loc: int, item):
738738

739739
def _concat(self, to_concat, name):
740740
# if calling index is category, don't check dtype of others
741-
return CategoricalIndex._concat_same_dtype(self, to_concat, name)
742-
743-
def _concat_same_dtype(self, to_concat, name):
744-
"""
745-
Concatenate to_concat which has the same class
746-
ValueError if other is not in the categories
747-
"""
748741
codes = np.concatenate([self._is_dtype_compat(c).codes for c in to_concat])
749742
result = self._create_from_codes(codes, name=name)
750743
# if name is None, _create_from_codes sets self.name

pandas/core/indexes/extension.py

-4
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,6 @@ def insert(self, loc: int, item):
236236
# ExtensionIndex subclasses must override Index.insert
237237
raise AbstractMethodError(self)
238238

239-
def _concat_same_dtype(self, to_concat, name):
240-
arr = type(self._data)._concat_same_type(to_concat)
241-
return type(self)._simple_new(arr, name=name)
242-
243239
def _get_unique_index(self, dropna=False):
244240
if self.is_unique and not dropna:
245241
return self

pandas/core/indexes/numeric.py

-4
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,6 @@ def _assert_safe_casting(cls, data, subarr):
147147
"""
148148
pass
149149

150-
def _concat_same_dtype(self, indexes, name):
151-
result = type(indexes[0])(np.concatenate([x._values for x in indexes]))
152-
return result.rename(name)
153-
154150
@property
155151
def is_all_dates(self) -> bool:
156152
"""

pandas/core/indexes/range.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -627,14 +627,18 @@ def join(self, other, how="left", level=None, return_indexers=False, sort=False)
627627

628628
return super().join(other, how, level, return_indexers, sort)
629629

630-
def _concat_same_dtype(self, indexes, name):
630+
def _concat(self, indexes, name):
631631
"""
632-
Concatenates multiple RangeIndex instances. All members of "indexes" must
633-
be of type RangeIndex; result will be RangeIndex if possible, Int64Index
634-
otherwise. E.g.:
632+
Overriding parent method for the case of all RangeIndex instances.
633+
634+
When all members of "indexes" are of type RangeIndex: result will be
635+
RangeIndex if possible, Int64Index otherwise. E.g.:
635636
indexes = [RangeIndex(3), RangeIndex(3, 6)] -> RangeIndex(6)
636637
indexes = [RangeIndex(3), RangeIndex(4, 6)] -> Int64Index([0,1,2,4,5])
637638
"""
639+
if not all(isinstance(x, RangeIndex) for x in indexes):
640+
return super()._concat(indexes, name)
641+
638642
start = step = next_ = None
639643

640644
# Filter the empty indexes

pandas/tests/arrays/test_datetimelike.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ def test_concat_same_type(self):
168168
arr = self.array_cls(idx)
169169

170170
result = arr._concat_same_type([arr[:-1], arr[1:], arr])
171-
expected = idx._concat_same_dtype([idx[:-1], idx[1:], idx], None)
171+
arr2 = arr.astype(object)
172+
expected = self.index_cls(np.concatenate([arr2[:-1], arr2[1:], arr2]), None)
172173

173174
tm.assert_index_equal(self.index_cls(result), expected)
174175

pandas/tests/indexes/categorical/test_category.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def test_append(self):
136136
tm.assert_index_equal(result, expected, exact=True)
137137

138138
def test_append_to_another(self):
139-
# hits Index._concat_same_dtype
139+
# hits Index._concat
140140
fst = Index(["a", "b"])
141141
snd = CategoricalIndex(["d", "e"])
142142
result = fst.append(snd)

0 commit comments

Comments
 (0)