8000 Merge pull request #8348 from anntzer/bincount-zero-minlength · juliantaylor/numpy@1a82adf · GitHub
[go: up one dir, main page]

Skip to content

Commit 1a82adf

Browse files
authored
Merge pull request numpy#8348 from anntzer/bincount-zero-minlength
ENH: Allow bincount(..., minlength=0).
2 parents b7dc519 + ec6d429 commit 1a82adf

File tree

3 files changed

+10
-11
lines changed

3 files changed

+10
-11
lines changed

numpy/add_newdocs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5107,7 +5107,7 @@ def luf(lamdaexpr, *args, **kwargs):
51075107

51085108
add_newdoc('numpy.core.multiarray', 'bincount',
51095109
"""
5110-
bincount(x, weights=None, minlength=None)
5110+
bincount(x, weights=None, minlength=0)
51115111
51125112
Count number of occurrences of each value in array of non-negative ints.
51135113

numpy/core/src/multiarray/compiled_base.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,10 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwds)
117117
}
118118
else {
119119
minlength = PyArray_PyIntAsIntp(mlength);
120-
if (minlength <= 0) {
120+
if (minlength < 0) {
121121
if (!PyErr_Occurred()) {
122122
PyErr_SetString(PyExc_ValueError,
123-
"minlength must be positive");
123+
"minlength must be non-negative");
124124
}
125125
goto fail;
126126
}

numpy/lib/tests/test_function_base.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2507,11 +2507,16 @@ def test_with_minlength(self):
25072507
x = np.array([0, 1, 0, 1, 1])
25082508
y = np.bincount(x, minlength=3)
25092509
assert_array_equal(y, np.array([2, 3, 0]))
2510+
x = []
2511+
y = np.bincount(x, minlength=0)
2512+
assert_array_equal(y, np.array([]))
25102513

25112514
def test_with_minlength_smaller_than_maxvalue(self):
25122515
x = np.array([0, 1, 1, 2, 2, 3, 3])
25132516
y = np.bincount(x, minlength=2)
25142517
assert_array_equal(y, np.array([1, 2, 2, 2]))
2518+
y = np.bincount(x, minlength=0)
2519+
assert_array_equal(y, np.array([1, 2, 2, 2]))
25152520

25162521
def test_with_minlength_and_weights(self):
25172522
x = np.array([1, 2, 4, 5, 2])
@@ -2535,22 +2540,16 @@ def test_with_incorrect_minlength(self):
25352540
"'str' object cannot be interpreted",
25362541
lambda: np.bincount(x, minlength="foobar"))
25372542
assert_raises_regex(ValueError,
2538-
"must be positive",
2543+
"must be non-negative",
25392544
lambda: np.bincount(x, minlength=-1))
2540-
assert_raises_regex(ValueError,
2541-
"must be positive",
2542-
lambda: np.bincount(x, minlength=0))
25432545

25442546
x = np.arange(5)
25452547
assert_raises_regex(TypeError,
25462548
"'str' object cannot be interpreted",
25472549
lambda: np.bincount(x, minlength="foobar"))
25482550
assert_raises_regex(ValueError,
2549-
"minlength must be positive",
2551+
"minlength must be non-negative",
25502552
lambda: np.bincount(x, minlength=-1))
2551-
assert_raises_regex(ValueError,
2552-
"minlength must be positive",
2553-
lambda: np.bincount(x, minlength=0))
25542553

25552554
@dec.skipif(not HAS_REFCOUNT, "python has no sys.getrefcount")
25562555
def test_dtype_reference_leaks(self):

0 commit comments

Comments
 (0)
0