8000 BUG: Fix partition and argpartition error for empty input. Closes #6530 by yashmehrotra · Pull Request #6553 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Fix partition and argpartition error for empty input. Closes #6530 #6553

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 3 commits into from
Oct 27, 2015
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
8 changes: 6 additions & 2 deletions numpy/core/src/multiarray/item_selection.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -978,13 +980,15 @@ _new_argsortlike(PyArrayObject *op, int axis, PyArray_ArgSortFunc *argsort,
if (needcopy) {
valbuffer = PyDataMem_NEW(N * elsize);
if (valbuffer == NULL) {
ret = -1;
goto fail;
}
}

if (needidxbuffer) {
idxbuffer = (npy_intp *)PyDataMem_NEW(N * sizeof(npy_intp));
if (idxbuffer == NULL) {
ret = -1;
goto fail;
}
}
Expand Down
18 changes: 18 additions & 0 deletions numpy/core/tests/test_item_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ def test_unicode_mode(self):
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()
4 changes: 4 additions & 0 deletions numpy/core/tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -2173,5 +2173,9 @@ def test_leak_in_structured_dtype_comparison(self):
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()
0