Skip to content

Commit 46826ad

Browse files
toobazjreback
authored andcommitted
REF: Special case NumericIndex._append_same_dtype() (#17307)
1 parent 4489389 commit 46826ad

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

asv_bench/benchmarks/index_object.py

+19
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,22 @@ def time_min(self):
219219

220220
def time_min_trivial(self):
221221
self.idx_inc.min()
222+
223+
224+
class IndexOps(object):
225+
goal_time = 0.2
226+
227+
def setup(self):
228+
N = 10000
229+
self.ridx = [RangeIndex(i * 100, (i + 1) * 100) for i in range(N)]
230+
self.iidx = [idx.astype(int) for idx in self.ridx]
231+
self.oidx = [idx.astype(str) for idx in self.iidx]
232+
233+
def time_concat_range(self):
234+
self.ridx[0].append(self.ridx[1:])
235+
236+
def time_concat_int(self):
237+
self.iidx[0].append(self.iidx[1:])
238+
239+
def time_concat_obj(self):
240+
self.oidx[0].append(self.oidx[1:])

pandas/core/dtypes/concat.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,11 @@ def _concat_datetimetz(to_concat, name=None):
467467
return to_concat[0]._simple_new(new_values, tz=tz, name=name)
468468

469469

470+
def _concat_index_same_dtype(indexes, klass=None):
471+
klass = klass if klass is not None else indexes[0].__class__
472+
return klass(np.concatenate([x._values for x in indexes]))
473+
474+
470475
def _concat_index_asobject(to_concat, name=None):
471476
"""
472477
concat all inputs as object. DatetimeIndex, TimedeltaIndex and
@@ -581,16 +586,15 @@ def _concat_rangeindex_same_dtype(indexes):
581586
elif step is None:
582587
# First non-empty index had only one element
583588
if obj._start == start:
584-
return _concat_index_asobject(indexes)
589+
from pandas import Int64Index
590+
return _concat_index_same_dtype(indexes, klass=Int64Index)
585591
step = obj._start - start
586592

587593
non_consecutive = ((step != obj._step and len(obj) > 1) or
588594
(next is not None and obj._start != next))
589595
if non_consecutive:
590-
# Int64Index._append_same_dtype([ix.astype(int) for ix in indexes])
591-
# would be preferred... but it currently resorts to
592-
# _concat_index_asobject anyway.
593-
return _concat_index_asobject(indexes)
596+
from pandas import Int64Index
597+
return _concat_index_same_dtype(indexes, klass=Int64Index)
594598

595599
if step is not None:
596600
next = obj[-1] + step

pandas/core/indexes/numeric.py

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from pandas.core.indexes.base import (
1818
Index, InvalidIndexError, _index_shared_docs)
1919
from pandas.util._decorators import Appender, cache_readonly
20+
import pandas.core.dtypes.concat as _concat
2021
import pandas.core.indexes.base as ibase
2122

2223

@@ -96,6 +97,9 @@ def _assert_safe_casting(cls, data, subarr):
9697
"""
9798
pass
9899

100+
def _concat_same_dtype(self, indexes, name):
101+
return _concat._concat_index_same_dtype(indexes).rename(name)
102+
99103
@property
100104
def is_all_dates(self):
101105
"""

0 commit comments

Comments
 (0)