Skip to content

Commit a87194b

Browse files
committed
PERF: performance regression in unioning multiple DatetimeIndex objects. #2336
1 parent fbd77d5 commit a87194b

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

pandas/core/index.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -2578,9 +2578,13 @@ def _union_indexes(indexes):
25782578

25792579
if kind == 'special':
25802580
result = indexes[0]
2581-
for other in indexes[1:]:
2582-
result = result.union(other)
2583-
return result
2581+
2582+
if hasattr(result, 'union_many'):
2583+
return result.union_many(indexes[1:])
2584+
else:
2585+
for other in indexes[1:]:
2586+
result = result.union(other)
2587+
return result
25842588
elif kind == 'array':
25852589
index = indexes[0]
25862590
for other in indexes[1:]:

pandas/src/inference.pyx

+1
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,7 @@ def map_infer_mask(ndarray arr, object f, ndarray[uint8_t] mask,
753753
convert_datetime=0)
754754

755755
return result
756+
756757
def map_infer(ndarray arr, object f, bint convert=1):
757758
'''
758759
Substitute for np.vectorize with pandas-friendly dtype inference

pandas/tseries/index.py

+31
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,37 @@ def union(self, other):
829829
result.offset = to_offset(result.inferred_freq)
830830
return result
831831

832+
def union_many(self, others):
833+
"""
834+
A bit of a hack to accelerate unioning a collection of indexes
835+
"""
836+
this = self
837+
838+
for other in others:
839+
if not isinstance(this, DatetimeIndex):
840+
this = Index.union(this, other)
841+
continue
842+
843+
if not isinstance(other, DatetimeIndex):
844+
try:
845+
other = DatetimeIndex(other)
846+
except TypeError:
847+
pass
848+
849+
this, other = this._maybe_utc_convert(other)
850+
851+
if this._can_fast_union(other):
852+
this = this._fast_union(other)
853+
else:
854+
tz = this.tz
855+
this = Index.union(this, other)
856+
if isinstance(this, DatetimeIndex):
857+
this.tz = tz
858+
859+
if this.freq is None:
860+
this.offset = to_offset(this.inferred_freq)
861+
return this
862+
832863
def join(self, other, how='left', level=None, return_indexers=False):
833864
"""
834865
See Index.join

0 commit comments

Comments
 (0)