Skip to content

Commit 9277f5d

Browse files
committed
ascending does not sort Booleans
1 parent 2a679db commit 9277f5d

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

src/_arraykit.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4885,18 +4885,21 @@ BIIterSelector_new(BlockIndexObject *bi,
48854885
}
48864886
}
48874887
else if (kind == BIIS_SEQ && k != 'i' && k != 'u') {
4888-
PyErr_SetString(PyExc_TypeError, "Arrays must integer kind");
4888+
PyErr_SetString(PyExc_TypeError, "Arrays must be integer kind");
48894889
return NULL;
48904890
}
48914891
else if (kind == BIIS_BOOLEAN && k != 'b') {
4892-
PyErr_SetString(PyExc_TypeError, "Arrays must Boolean kind");
4892+
PyErr_SetString(PyExc_TypeError, "Arrays must be Boolean kind");
48934893
return NULL;
48944894
}
4895-
if (kind == BIIS_BOOLEAN && len != bi->bir_count) {
4896-
PyErr_SetString(PyExc_TypeError, "Boolean arrays must match BlockIndex size");
4897-
return NULL;
4895+
4896+
if (kind == BIIS_BOOLEAN) {
4897+
if (len != bi->bir_count) {
4898+
PyErr_SetString(PyExc_TypeError, "Boolean arrays must match BlockIndex size");
4899+
return NULL;
4900+
}
48984901
}
4899-
if (ascending) {
4902+
else if (ascending) { // not Boolean
49004903
// NOTE: we can overwrite selector here as we have a borrowed refernce; sorting gives us a new reference, so we do not need to incref below
49014904
selector = PyArray_NewCopy(a, NPY_CORDER);
49024905
// sort in-place; can use a non-stable sort

test/test_block_index.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,3 +761,36 @@ def test_block_index_iter_contiguous_g(self) -> None:
761761

762762
with self.assertRaises(TypeError):
763763
_ = list(bi1.iter_contiguous('a'))
764+
765+
766+
def test_block_index_iter_contiguous_h1(self) -> None:
767+
bi1 = BlockIndex()
768+
bi1.register(np.arange(6).reshape(2,3))
769+
bi1.register(np.arange(6).reshape(2,3))
770+
771+
sel = np.array([1, 1, 1, 0, 0, 0]).astype(bool)
772+
post1 = list(bi1.iter_contiguous(sel))
773+
post2 = list(bi1.iter_contiguous(sel, ascending=True))
774+
self.assertEqual(post1, post2)
775+
self.assertEqual(post1, [(0, slice(0, 3, None))])
776+
777+
def test_block_index_iter_contiguous_h2(self) -> None:
778+
bi1 = BlockIndex()
779+
bi1.register(np.arange(6).reshape(2,3))
780+
bi1.register(np.arange(6).reshape(2,3))
781+
782+
sel = np.array([1, 0, 1, 0, 1, 0]).astype(bool)
783+
post1 = list(bi1.iter_contiguous(sel))
784+
post2 = list(bi1.iter_contiguous(sel, ascending=True))
785+
self.assertEqual(post1, post2)
786+
self.assertEqual(post1,
787+
[(0, slice(0, 1, None)),
788+
(0, slice(2, 3, None)),
789+
(1, slice(1, 2, None))])
790+
791+
post3 = list(bi1.iter_contiguous(sel, ascending=True, reduce=True))
792+
self.assertEqual(post3,
793+
[(0, 0),
794+
(0, 2),
795+
(1, 1)])
796+

0 commit comments

Comments
 (0)