Skip to content

Commit 576a696

Browse files
committed
REF: reuse general code for Index appending
1 parent 7b3cecb commit 576a696

File tree

3 files changed

+30
-43
lines changed

3 files changed

+30
-43
lines changed

pandas/core/indexes/base.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,23 +1680,27 @@ def append(self, other):
16801680
names = set([obj.name for obj in to_concat])
16811681
name = None if len(names) > 1 else self.name
16821682

1683-
if self.is_categorical():
1684-
# if calling index is category, don't check dtype of others
1685-
from pandas.core.indexes.category import CategoricalIndex
1686-
return CategoricalIndex._append_same_dtype(self, to_concat, name)
1683+
res = self._concat(to_concat)
16871684

1688-
typs = _concat.get_dtype_kinds(to_concat)
1685+
res.name = name
1686+
return res
16891687

1688+
def _concat(self, to_concat):
1689+
"""
1690+
Concatenate to_concat to single Index
1691+
"""
1692+
typs = _concat.get_dtype_kinds(to_concat)
16901693
if len(typs) == 1:
1691-
return self._append_same_dtype(to_concat, name=name)
1692-
return _concat._concat_index_asobject(to_concat, name=name)
1694+
return self._concat_same_dtype(to_concat)
1695+
else:
1696+
return _concat._concat_index_asobject(to_concat)
16931697

1694-
def _append_same_dtype(self, to_concat, name):
1698+
def _concat_same_dtype(self, to_concat):
16951699
"""
1696-
Concatenate to_concat which has the same class
1700+
Concatenate to_concat which has the same dtype
16971701
"""
16981702
# must be overrided in specific classes
1699-
return _concat._concat_index_asobject(to_concat, name)
1703+
return _concat._concat_index_asobject(to_concat)
17001704

17011705
_index_shared_docs['take'] = """
17021706
return a new %(klass)s of the values selected by the indices

pandas/core/indexes/category.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,13 @@ def _convert_arr_indexer(self, keyarr):
546546
def _convert_index_indexer(self, keyarr):
547547
return self._shallow_copy(keyarr)
548548

549+
def _concat(self, to_concat):
550+
"""
551+
Concatenate to_concat to single Index
552+
"""
553+
# if calling index is category, don't check dtype of others
554+
return self._concat_same_dtype(to_concat)
555+
549556
@Appender(_index_shared_docs['take'] % _index_doc_kwargs)
550557
def take(self, indices, axis=0, allow_fill=True,
551558
fill_value=None, **kwargs):
@@ -615,17 +622,14 @@ def insert(self, loc, item):
615622
codes = np.concatenate((codes[:loc], code, codes[loc:]))
616623
return self._create_from_codes(codes)
617624

618-
def _append_same_dtype(self, to_concat, name):
625+
def _concat_same_dtype(self, to_concat):
619626
"""
620627
Concatenate to_concat which has the same class
621628
ValueError if other is not in the categories
622629
"""
623630
to_concat = [self._is_dtype_compat(c) for c in to_concat]
624631
codes = np.concatenate([c.codes for c in to_concat])
625-
result = self._create_from_codes(codes, name=name)
626-
# if name is None, _create_from_codes sets self.name
627-
result.name = name
628-
return result
632+
return self._create_from_codes(codes)
629633

630634
def _codes_for_groupby(self, sort):
631635
""" Return a Categorical adjusted for groupby """

pandas/core/indexes/range.py

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -443,33 +443,14 @@ def join(self, other, how='left', level=None, return_indexers=False,
443443
return super(RangeIndex, self).join(other, how, level, return_indexers,
444444
sort)
445445

446-
def append(self, other):
447-
"""
448-
Append a collection of Index options together
449-
450-
Parameters
451-
----------
452-
other : Index or list/tuple of indices
453-
454-
Returns
455-
-------
456-
appended : RangeIndex if all indexes are consecutive RangeIndexes,
457-
otherwise Int64Index or Index
458-
"""
459-
460-
to_concat = [self]
461-
462-
if isinstance(other, (list, tuple)):
463-
to_concat = to_concat + list(other)
464-
else:
465-
to_concat.append(other)
446+
def _concat_same_dtype(self, indexes):
466447

467-
if not all([isinstance(i, RangeIndex) for i in to_concat]):
468-
return super(RangeIndex, self).append(other)
448+
if not all([isinstance(i, RangeIndex) for i in indexes]):
449+
return super(RangeIndex, self)._concat_same_dtype(indexes)
469450

470451
start = step = next = None
471452

472-
for obj in to_concat:
453+
for obj in indexes:
473454
if not len(obj):
474455
continue
475456

@@ -481,13 +462,13 @@ def append(self, other):
481462
elif step is None:
482463
# First non-empty index had only one element
483464
if obj._start == start:
484-
return super(RangeIndex, self).append(other)
465+
return super(RangeIndex, self)._concat_same_dtype(indexes)
485466
step = obj._start - start
486467

487468
non_consecutive = ((step != obj._step and len(obj) > 1) or
488469
(next is not None and obj._start != next))
489470
if non_consecutive:
490-
return super(RangeIndex, self).append(other)
471+
return super(RangeIndex, self)._concat_same_dtype(indexes)
491472

492473
if step is not None:
493474
next = obj[-1] + step
@@ -496,9 +477,7 @@ def append(self, other):
496477
start = obj._start
497478
step = obj._step
498479
stop = obj._stop if next is None else next
499-
names = set([obj.name for obj in to_concat])
500-
name = None if len(names) > 1 else self.name
501-
return RangeIndex(start, stop, step, name=name)
480+
return RangeIndex(start, stop, step)
502481

503482
def __len__(self):
504483
"""

0 commit comments

Comments
 (0)