Skip to content

Commit 2794474

Browse files
jschendeljreback
authored andcommitted
BUG: Add uint64 support to IntervalTree (#20651)
1 parent fa24af9 commit 2794474

File tree

3 files changed

+37
-33
lines changed

3 files changed

+37
-33
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,7 @@ Indexing
10671067
- Bug in ``Index`` subclasses constructors that ignore unexpected keyword arguments (:issue:`19348`)
10681068
- Bug in :meth:`Index.difference` when taking difference of an ``Index`` with itself (:issue:`20040`)
10691069
- Bug in :meth:`DataFrame.first_valid_index` and :meth:`DataFrame.last_valid_index` in presence of entire rows of NaNs in the middle of values (:issue:`20499`).
1070+
- Bug in :class:`IntervalIndex` where some indexing operations were not supported for overlapping or non-monotonic ``uint64`` data (:issue:`20636`)
10701071

10711072
MultiIndex
10721073
^^^^^^^^^^

pandas/_libs/intervaltree.pxi.in

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ WARNING: DO NOT edit .pxi FILE directly, .pxi is generated from .pxi.in
55
"""
66

77
from numpy cimport (
8-
int64_t, int32_t, float64_t, float32_t,
8+
int64_t, int32_t, float64_t, float32_t, uint64_t,
99
ndarray,
1010
PyArray_ArgSort, NPY_QUICKSORT, PyArray_Take)
1111
import numpy as np
@@ -24,6 +24,7 @@ ctypedef fused scalar_t:
2424
float32_t
2525
int64_t
2626
int32_t
27+
uint64_t
2728

2829

2930
#----------------------------------------------------------------------
@@ -205,7 +206,7 @@ cdef sort_values_and_indices(all_values, all_indices, subset):
205206
{{py:
206207

207208
nodes = []
208-
for dtype in ['float32', 'float64', 'int32', 'int64']:
209+
for dtype in ['float32', 'float64', 'int32', 'int64', 'uint64']:
209210
for closed, cmp_left, cmp_right in [
210211
('left', '<=', '<'),
211212
('right', '<', '<='),

pandas/tests/indexes/interval/test_interval_tree.py

+33-31
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,37 @@ def closed(request):
1212
return request.param
1313

1414

15+
@pytest.fixture(
16+
scope='class', params=['int32', 'int64', 'float32', 'float64', 'uint64'])
17+
def dtype(request):
18+
return request.param
19+
20+
21+
@pytest.fixture(scope='class')
22+
def tree(dtype):
23+
left = np.arange(5, dtype=dtype)
24+
return IntervalTree(left, left + 2)
25+
26+
1527
class TestIntervalTree(object):
16-
def setup_method(self, method):
17-
def gentree(dtype):
18-
left = np.arange(5, dtype=dtype)
19-
right = left + 2
20-
return IntervalTree(left, right)
21-
22-
self.tree = gentree('int64')
23-
self.trees = {dtype: gentree(dtype)
24-
for dtype in ['int32', 'int64', 'float32', 'float64']}
25-
26-
def test_get_loc(self):
27-
for dtype, tree in self.trees.items():
28-
tm.assert_numpy_array_equal(tree.get_loc(1),
29-
np.array([0], dtype='int64'))
30-
tm.assert_numpy_array_equal(np.sort(tree.get_loc(2)),
31-
np.array([0, 1], dtype='int64'))
32-
with pytest.raises(KeyError):
33-
tree.get_loc(-1)
34-
35-
def test_get_indexer(self):
36-
for dtype, tree in self.trees.items():
37-
tm.assert_numpy_array_equal(
38-
tree.get_indexer(np.array([1.0, 5.5, 6.5])),
39-
np.array([0, 4, -1], dtype='int64'))
40-
with pytest.raises(KeyError):
41-
tree.get_indexer(np.array([3.0]))
42-
43-
def test_get_indexer_non_unique(self):
44-
indexer, missing = self.tree.get_indexer_non_unique(
28+
29+
def test_get_loc(self, tree):
30+
tm.assert_numpy_array_equal(tree.get_loc(1),
31+
np.array([0], dtype='int64'))
32+
tm.assert_numpy_array_equal(np.sort(tree.get_loc(2)),
33+
np.array([0, 1], dtype='int64'))
34+
with pytest.raises(KeyError):
35+
tree.get_loc(-1)
36+
37+
def test_get_indexer(self, tree):
38+
tm.assert_numpy_array_equal(
39+
tree.get_indexer(np.array([1.0, 5.5, 6.5])),
40+
np.array([0, 4, -1], dtype='int64'))
41+
with pytest.raises(KeyError):
42+
tree.get_indexer(np.array([3.0]))
43+
44+
def test_get_indexer_non_unique(self, tree):
45+
indexer, missing = tree.get_indexer_non_unique(
4546
np.array([1.0, 2.0, 6.5]))
4647
tm.assert_numpy_array_equal(indexer[:1],
4748
np.array([0], dtype='int64'))
@@ -51,8 +52,9 @@ def test_get_indexer_non_unique(self):
5152
np.array([-1], dtype='int64'))
5253
tm.assert_numpy_array_equal(missing, np.array([2], dtype='int64'))
5354

54-
def test_duplicates(self):
55-
tree = IntervalTree([0, 0, 0], [1, 1, 1])
55+
def test_duplicates(self, dtype):
56+
left = np.array([0, 0, 0], dtype=dtype)
57+
tree = IntervalTree(left, left + 1)
5658
tm.assert_numpy_array_equal(np.sort(tree.get_loc(0.5)),
5759
np.array([0, 1, 2], dtype='int64'))
5860

0 commit comments

Comments
 (0)