Skip to content

Commit ae61c54

Browse files
jschendelPingviinituutti
authored andcommitted
BUG: Fix IntervalTree handling of NaN (pandas-dev#23353)
1 parent 487ba64 commit ae61c54

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,7 @@ Interval
11331133
- Bug in the ``IntervalIndex`` repr where a trailing comma was missing after the list of intervals (:issue:`20611`)
11341134
- Bug in :class:`Interval` where scalar arithmetic operations did not retain the ``closed`` value (:issue:`22313`)
11351135
- Bug in :class:`IntervalIndex` where indexing with datetime-like values raised a ``KeyError`` (:issue:`20636`)
1136+
- Bug in ``IntervalTree`` where data containing ``NaN`` triggered a warning and resulted in incorrect indexing queries with :class:`IntervalIndex` (:issue:`23352`)
11361137

11371138
Indexing
11381139
^^^^^^^^

pandas/_libs/intervaltree.pxi.in

+6
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ cdef class IntervalTree(IntervalMixin):
7272

7373
self.closed = closed
7474

75+
# GH 23352: ensure no nan in nodes
76+
mask = ~np.isnan(self.left)
77+
self.left = self.left[mask]
78+
self.right = self.right[mask]
79+
indices = indices[mask]
80+
7581
node_cls = NODE_CLASSES[str(self.dtype), closed]
7682
self.root = node_cls(self.left, self.right, indices, leaf_size)
7783

pandas/tests/indexes/interval/test_interval_tree.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,22 @@ def dtype(request):
1414
return request.param
1515

1616

17-
@pytest.fixture(scope='class')
18-
def tree(dtype):
19-
left = np.arange(5, dtype=dtype)
20-
return IntervalTree(left, left + 2)
17+
@pytest.fixture(params=[1, 2, 10])
18+
def leaf_size(request):
19+
return request.param
20+
21+
22+
@pytest.fixture(params=[
23+
np.arange(5, dtype='int64'),
24+
np.arange(5, dtype='int32'),
25+
np.arange(5, dtype='uint64'),
26+
np.arange(5, dtype='float64'),
27+
np.arange(5, dtype='float32'),
28+
np.array([0, 1, 2, 3, 4, np.nan], dtype='float64'),
29+
np.array([0, 1, 2, 3, 4, np.nan], dtype='float32')])
30+
def tree(request, leaf_size):
31+
left = request.param
32+
return IntervalTree(left, left + 2, leaf_size=leaf_size)
2133

2234

2335
class TestIntervalTree(object):

0 commit comments

Comments
 (0)