Skip to content
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

BUG: Fix IntervalTree handling of NaN #23353

Merged
merged 2 commits into from
Oct 30, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 16 additions & 3 deletions pandas/tests/indexes/interval/test_interval_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,27 @@ def dtype(request):
return request.param


@pytest.fixture(scope='class')
def tree(dtype):
left = np.arange(5, dtype=dtype)
@pytest.fixture(scope='class', 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):
left = request.param
return IntervalTree(left, left + 2)


class TestIntervalTree(object):

def test_construction_nan(self):
# GH 23352
left, right = [0, 1, 2, np.nan], [1, 2, 3, np.nan]
with tm.assert_produces_warning(None):
IntervalTree(left, right, leaf_size=2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what exactly does this produce though? are the nan's valid? you are just dropping them.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intent here was to verify that the RuntimeWarning described in the issue is not occurring. I guess this test isn't really necessary, as at least one CI instance is set to fail if warnings are raised. Removed this test, and instead parametrized the tree fixture to include leaf_size that will trigger the warning and cause the invalid behavior described in the issue (so beyond just a warning would cause the asserts to fail if there's a regression).


def test_get_loc(self, tree):
tm.assert_numpy_array_equal(tree.get_loc(1),
np.array([0], dtype='int64'))
Expand Down