From 839225599b1fb18ebb09424459ebd2066a8f21f9 Mon Sep 17 00:00:00 2001 From: jaimefrio Date: Thu, 30 Jan 2014 16:49:55 -0800 Subject: BUG: check for monotonic bin arrays in digitize The check for monotonic bin arrays of digitize doesn't properly handle inputs with repeated entries at the beginning of the array: ``` >>> np.__version__ '1.8.0' >>> np.digitize([1], [0, 0 , 2]) array([2], dtype=int64) >>> np.digitize([1], [2, 2, 0]) Traceback (most recent call last): File "", line 1, in ValueError: The bins must be monotonically increasing or decreasing ``` Modified `check_array_monotonic` in `_compiled_base.c` to skip over repeating entries before deciding to check for increasing or decreasing monotonicity and added relevant tests to `test_function_base.py`. --- numpy/lib/tests/test_function_base.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'numpy/lib/tests/test_function_base.py') diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 494b512f7..0d2a66f53 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -760,6 +760,19 @@ class TestDigitize(TestCase): x = rand(10) bins = np.linspace(x.min(), x.max(), 10) assert_(np.all(digitize(x, bins, True) != 10)) + + def test_monotonic(self): + x = [0] + bins = [0, 0, 1] + digitize(x, bins) + bins = [1, 1, 0] + digitize(x, bins) + bins = [1, 1, 1, 1] + digitize(x, bins) + bins = [0, 0, 1, 0] + assert_raises(ValueError, digitize, x, bins) + bins = [1, 1, 0, 1] + assert_raises(ValueError, digitize, x, bins) class TestUnwrap(TestCase): -- cgit v1.2.1