diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2015-10-27 14:43:28 -0600 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2015-10-27 14:43:28 -0600 |
commit | c0e48cfbbdef9cca954b0c4edd0052e1ec8a30aa (patch) | |
tree | f5652bbc53cbe8a915bff9ec39789dabedc5f3c6 | |
parent | 522a0f749d1ac8d0acece68f9c65d46e96529407 (diff) | |
parent | 4d9bf8a3aa2f2bfe37e1e32fb036fafa156a6d38 (diff) | |
download | numpy-c0e48cfbbdef9cca954b0c4edd0052e1ec8a30aa.tar.gz |
Merge pull request #6553 from yashmehrotra/partition-fix
BUG: Fix partition and argpartition error for empty input. Closes #6530
-rw-r--r-- | numpy/core/src/multiarray/item_selection.c | 8 | ||||
-rw-r--r-- | numpy/core/tests/test_item_selection.py | 18 | ||||
-rw-r--r-- | numpy/core/tests/test_regression.py | 4 |
3 files changed, 28 insertions, 2 deletions
diff --git a/numpy/core/src/multiarray/item_selection.c b/numpy/core/src/multiarray/item_selection.c index ec0717bd6..64fa70b6d 100644 --- a/numpy/core/src/multiarray/item_selection.c +++ b/numpy/core/src/multiarray/item_selection.c @@ -809,7 +809,7 @@ _new_sortlike(PyArrayObject *op, int axis, PyArray_SortFunc *sort, PyArrayIterObject *it; npy_intp size; - int ret = -1; + int ret = 0; NPY_BEGIN_THREADS_DEF; @@ -829,6 +829,7 @@ _new_sortlike(PyArrayObject *op, int axis, PyArray_SortFunc *sort, if (needcopy) { buffer = PyDataMem_NEW(N * elsize); if (buffer == NULL) { + ret = -1; goto fail; } } @@ -947,7 +948,7 @@ _new_argsortlike(PyArrayObject *op, int axis, PyArray_ArgSortFunc *argsort, PyArrayIterObject *it, *rit; npy_intp size; - int ret = -1; + int ret = 0; NPY_BEGIN_THREADS_DEF; @@ -969,6 +970,7 @@ _new_argsortlike(PyArrayObject *op, int axis, PyArray_ArgSortFunc *argsort, it = (PyArrayIterObject *)PyArray_IterAllButAxis((PyObject *)op, &axis); rit = (PyArrayIterObject *)PyArray_IterAllButAxis((PyObject *)rop, &axis); if (it == NULL || rit == NULL) { + ret = -1; goto fail; } size = it->size; @@ -978,6 +980,7 @@ _new_argsortlike(PyArrayObject *op, int axis, PyArray_ArgSortFunc *argsort, if (needcopy) { valbuffer = PyDataMem_NEW(N * elsize); if (valbuffer == NULL) { + ret = -1; goto fail; } } @@ -985,6 +988,7 @@ _new_argsortlike(PyArrayObject *op, int axis, PyArray_ArgSortFunc *argsort, if (needidxbuffer) { idxbuffer = (npy_intp *)PyDataMem_NEW(N * sizeof(npy_intp)); if (idxbuffer == NULL) { + ret = -1; goto fail; } } diff --git a/numpy/core/tests/test_item_selection.py b/numpy/core/tests/test_item_selection.py index f3e7701d4..5e9cadd8f 100644 --- a/numpy/core/tests/test_item_selection.py +++ b/numpy/core/tests/test_item_selection.py @@ -68,6 +68,24 @@ class TestTake(TestCase): k = b'\xc3\xa4'.decode("UTF8") assert_raises(ValueError, d.take, 5, mode=k) + def test_empty_partition(self): + # In reference to github issue #6530 + a_original = np.array([0, 2, 4, 6, 8, 10]) + a = a_original.copy() + + # An empty partition should be a successful no-op + a.partition(np.array([], dtype=np.int16)) + + assert_array_equal(a, a_original) + + def test_empty_argpartition(self): + # In reference to github issue #6530 + a = np.array([0, 2, 4, 6, 8, 10]) + a = a.argpartition(np.array([], dtype=np.int16)) + + b = np.array([0, 1, 2, 3, 4, 5]) + assert_array_equal(a, b) + if __name__ == "__main__": run_module_suite() diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py index 3aba71463..ac34cfa53 100644 --- a/numpy/core/tests/test_regression.py +++ b/numpy/core/tests/test_regression.py @@ -2173,5 +2173,9 @@ class TestRegression(TestCase): after = sys.getrefcount(a) assert_equal(before, after) + def test_empty_percentile(self): + # gh-6530 / gh-6553 + assert_array_equal(np.percentile(np.arange(10), []), np.array([])) + if __name__ == "__main__": run_module_suite() |