Skip to content

BUG: Fix IntervalTree handling of NaN #23353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,7 @@ Interval
- Bug in the ``IntervalIndex`` repr where a trailing comma was missing after the list of intervals (:issue:`20611`)
- Bug in :class:`Interval` where scalar arithmetic operations did not retain the ``closed`` value (:issue:`22313`)
- Bug in :class:`IntervalIndex` where indexing with datetime-like values raised a ``KeyError`` (:issue:`20636`)
- Bug in ``IntervalTree`` where data containing ``NaN`` triggered a warning and resulted in incorrect indexing queries with :class:`IntervalIndex` (:issue:`23352`)

Indexing
^^^^^^^^
Expand Down
6 changes: 6 additions & 0 deletions pandas/_libs/intervaltree.pxi.in
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ cdef class IntervalTree(IntervalMixin):

self.closed = closed

# GH 23352: ensure no nan in nodes
mask = ~np.isnan(self.left)
self.left = self.left[mask]
self.right = self.right[mask]
indices = indices[mask]

node_cls = NODE_CLASSES[str(self.dtype), closed]
self.root = node_cls(self.left, self.right, indices, leaf_size)

Expand Down
20 changes: 16 additions & 4 deletions pandas/tests/indexes/interval/test_interval_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,22 @@ def dtype(request):
return request.param


@pytest.fixture(scope='class')
def tree(dtype):
left = np.arange(5, dtype=dtype)
return IntervalTree(left, left + 2)
@pytest.fixture(params=[1, 2, 10])
def leaf_size(request):
return request.param


@pytest.fixture(params=[
np.arange(5, dtype='int64'),
np.arange(5, dtype='int32'),
np.arange(5, dtype='uint64'),
np.arange(5, dtype='float64'),
np.arange(5, dtype='float32'),
np.array([0, 1, 2, 3, 4, np.nan], dtype='float64'),
np.array([0, 1, 2, 3, 4, np.nan], dtype='float32')])
def tree(request, leaf_size):
left = request.param
return IntervalTree(left, left + 2, leaf_size=leaf_size)


class TestIntervalTree(object):
Expand Down