Skip to content

Commit b718d3b

Browse files
meeseeksmachinejreback
authored andcommitted
Backport PR #25498: BUG: Fix RecursionError during IntervalTree construction (#25519)
1 parent 03fe61d commit b718d3b

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

doc/source/whatsnew/v0.24.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ Bug Fixes
9898

9999
- Bug in :meth:`Series.is_unique` where single occurrences of ``NaN`` were not considered unique (:issue:`25180`)
100100
- Bug in :func:`merge` when merging an empty ``DataFrame`` with an ``Int64`` column or a non-empty ``DataFrame`` with an ``Int64`` column that is all ``NaN`` (:issue:`25183`)
101+
- Bug in ``IntervalTree`` where a ``RecursionError`` occurs upon construction due to an overflow when adding endpoints, which also causes :class:`IntervalIndex` to crash during indexing operations (:issue:`25485`)
101102
-
102103

103104
.. _whatsnew_0.242.contributors:

pandas/_libs/intervaltree.pxi.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ cdef class {{dtype_title}}Closed{{closed_title}}IntervalNode:
284284
else:
285285
# calculate a pivot so we can create child nodes
286286
self.is_leaf_node = False
287-
self.pivot = np.median(left + right) / 2
287+
self.pivot = np.median(left / 2 + right / 2)
288288
left_set, right_set, center_set = self.classify_intervals(
289289
left, right)
290290

pandas/tests/indexes/interval/test_interval_tree.py

+10
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,13 @@ def test_is_overlapping_trivial(self, closed, left, right):
171171
# GH 23309
172172
tree = IntervalTree(left, right, closed=closed)
173173
assert tree.is_overlapping is False
174+
175+
def test_construction_overflow(self):
176+
# GH 25485
177+
left, right = np.arange(101), [np.iinfo(np.int64).max] * 101
178+
tree = IntervalTree(left, right)
179+
180+
# pivot should be average of left/right medians
181+
result = tree.root.pivot
182+
expected = (50 + np.iinfo(np.int64).max) / 2
183+
assert result == expected

0 commit comments

Comments
 (0)