diff options
Diffstat (limited to 'numpy/lib')
-rw-r--r-- | numpy/lib/src/_compiled_base.c | 20 | ||||
-rw-r--r-- | numpy/lib/tests/test_function_base.py | 11 | ||||
-rw-r--r-- | numpy/lib/tests/test_regression.py | 4 |
3 files changed, 27 insertions, 8 deletions
diff --git a/numpy/lib/src/_compiled_base.c b/numpy/lib/src/_compiled_base.c index 82343fc35..a66e5da4c 100644 --- a/numpy/lib/src/_compiled_base.c +++ b/numpy/lib/src/_compiled_base.c @@ -124,16 +124,29 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwds) kwlist, &list, &weight, &mlength)) { goto fail; } + lst = (PyArrayObject *)PyArray_ContiguousFromAny(list, NPY_INTP, 1, 1); if (lst == NULL) { goto fail; } len = PyArray_SIZE(lst); + type = PyArray_DescrFromType(NPY_INTP); + + /* handle empty list */ if (len < 1) { - PyErr_SetString(PyExc_ValueError, - "The first argument cannot be empty."); - goto fail; + if (mlength == Py_None) { + minlength = 0; + } + else if (!(minlength = PyArray_PyIntAsIntp(mlength))) { + goto fail; + } + if (!(ans = PyArray_Zeros(1, &minlength, type, 0))){ + goto fail; + } + Py_DECREF(lst); + return ans; } + numbers = (npy_intp *) PyArray_DATA(lst); mxi = mxx(numbers, len); mni = mnx(numbers, len); @@ -157,7 +170,6 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwds) ans_size = minlength; } } - type = PyArray_DescrFromType(NPY_INTP); if (weight == Py_None) { ans = (PyArrayObject *)PyArray_Zeros(1, &ans_size, type, 0); if (ans == NULL) { diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index a7d501c52..145c83b77 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -1131,6 +1131,17 @@ class TestBincount(TestCase): y = np.bincount(x, w, 8) assert_array_equal(y, np.array([0, 0.2, 0.5, 0, 0.5, 0.1, 0, 0])) + def test_empty(self): + x = np.array([], dtype=int) + y = np.bincount(x) + assert_array_equal(x,y) + + def test_empty_with_minlength(self): + x = np.array([], dtype=int) + y = np.bincount(x, minlength=5) + assert_array_equal(y, np.zeros(5, dtype=int)) + + class TestInterp(TestCase): def test_exceptions(self): assert_raises(ValueError, interp, 0, [], []) diff --git a/numpy/lib/tests/test_regression.py b/numpy/lib/tests/test_regression.py index c244aea87..71400d112 100644 --- a/numpy/lib/tests/test_regression.py +++ b/numpy/lib/tests/test_regression.py @@ -169,10 +169,6 @@ class TestRegression(TestCase): sys.stdout.close() sys.stdout = oldstdout - def test_bincount_empty(self): - """Ticket #1387: empty array as input for bincount.""" - assert_raises(ValueError, lambda : np.bincount(np.array([], dtype=np.intp))) - def test_include_dirs(self): """As a sanity check, just test that get_include and get_numarray_include include something reasonable. Somewhat |