summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2017-10-18 12:46:46 -0600
committerGitHub <noreply@github.com>2017-10-18 12:46:46 -0600
commit0984465a613c6ca6038d53b0845b4bc1a7368e8a (patch)
tree74966949aa3e00b40c76ac5a63919e96a385d145
parentaca47d713624bb09e73db014811f8c1be31dfdcd (diff)
parentf9b99715be1844ce4c612dc66275f1bc662a0af8 (diff)
downloadnumpy-0984465a613c6ca6038d53b0845b4bc1a7368e8a.tar.gz
Merge pull request #9065 from eric-wieser/deprecate-bincount
DEP: 0 should be passed to bincount, not None
-rw-r--r--doc/release/1.14.0-notes.rst2
-rw-r--r--numpy/core/src/multiarray/compiled_base.c31
-rw-r--r--numpy/core/tests/test_deprecations.py6
-rw-r--r--numpy/lib/tests/test_function_base.py4
4 files changed, 31 insertions, 12 deletions
diff --git a/doc/release/1.14.0-notes.rst b/doc/release/1.14.0-notes.rst
index f576923b2..130498530 100644
--- a/doc/release/1.14.0-notes.rst
+++ b/doc/release/1.14.0-notes.rst
@@ -23,6 +23,8 @@ Deprecations
* Truth testing on an empty array is deprecated. To check if an array is not
empty, use ``array.size > 0``.
+* Calling ``np.bincount`` with ``minlength=None`` is deprecated - instead,
+ ``minlength=0`` should be used.
Future Changes
==============
diff --git a/numpy/core/src/multiarray/compiled_base.c b/numpy/core/src/multiarray/compiled_base.c
index 580578562..95b1d241a 100644
--- a/numpy/core/src/multiarray/compiled_base.c
+++ b/numpy/core/src/multiarray/compiled_base.c
@@ -96,9 +96,10 @@ minmax(const npy_intp *data, npy_intp data_len, npy_intp *mn, npy_intp *mx)
NPY_NO_EXPORT PyObject *
arr_bincount(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwds)
{
- PyObject *list = NULL, *weight = Py_None, *mlength = Py_None;
+ PyObject *list = NULL, *weight = Py_None, *mlength = NULL;
PyArrayObject *lst = NULL, *ans = NULL, *wts = NULL;
- npy_intp *numbers, *ians, len, mx, mn, ans_size, minlength;
+ npy_intp *numbers, *ians, len, mx, mn, ans_size;
+ npy_intp minlength = 0;
npy_intp i;
double *weights , *dans;
static char *kwlist[] = {"list", "weights", "minlength", NULL};
@@ -114,20 +115,30 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwds)
}
len = PyArray_SIZE(lst);
+ /*
+ * This if/else if can be removed by changing the argspec to O|On above,
+ * once we retire the deprecation
+ */
if (mlength == Py_None) {
- minlength = 0;
+ /* NumPy 1.14, 2017-06-01 */
+ if (DEPRECATE("0 should be passed as minlength instead of None; "
+ "this will error in future.") < 0) {
+ goto fail;
+ }
}
- else {
+ else if (mlength != NULL) {
minlength = PyArray_PyIntAsIntp(mlength);
- if (minlength < 0) {
- if (!PyErr_Occurred()) {
- PyErr_SetString(PyExc_ValueError,
- "minlength must be non-negative");
- }
+ if (error_converting(minlength)) {
goto fail;
}
}
+ if (minlength < 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "'minlength' must not be negative");
+ goto fail;
+ }
+
/* handle empty list */
if (len == 0) {
ans = (PyArrayObject *)PyArray_ZEROS(1, &minlength, NPY_INTP, 0);
@@ -142,7 +153,7 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwds)
minmax(numbers, len, &mn, &mx);
if (mn < 0) {
PyErr_SetString(PyExc_ValueError,
- "The first argument of bincount must be non-negative");
+ "'list' argument must have no negative elements");
goto fail;
}
ans_size = mx + 1;
diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py
index d7c402b53..1c1851fc7 100644
--- a/numpy/core/tests/test_deprecations.py
+++ b/numpy/core/tests/test_deprecations.py
@@ -463,5 +463,11 @@ class TestTruthTestingEmptyArrays(_DeprecationTestCase):
self.assert_deprecated(bool, args=(np.zeros((0, 0)),))
+class TestBincount(_DeprecationTestCase):
+ # 2017-06-01, 1.14.0
+ def test_bincount_minlength(self):
+ self.assert_deprecated(lambda: np.bincount([1, 2, 3], minlength=None))
+
+
if __name__ == "__main__":
run_module_suite()
diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py
index 10440d97c..39edc18b4 100644
--- a/numpy/lib/tests/test_function_base.py
+++ b/numpy/lib/tests/test_function_base.py
@@ -2623,7 +2623,7 @@ class TestBincount(object):
"'str' object cannot be interpreted",
lambda: np.bincount(x, minlength="foobar"))
assert_raises_regex(ValueError,
- "must be non-negative",
+ "must not be negative",
lambda: np.bincount(x, minlength=-1))
x = np.arange(5)
@@ -2631,7 +2631,7 @@ class TestBincount(object):
"'str' object cannot be interpreted",
lambda: np.bincount(x, minlength="foobar"))
assert_raises_regex(ValueError,
- "minlength must be non-negative",
+ "must not be negative",
lambda: np.bincount(x, minlength=-1))
@dec.skipif(not HAS_REFCOUNT, "python has no sys.getrefcount")