diff options
author | Robert Kern <rkern@enthought.com> | 2016-05-25 14:11:27 +0100 |
---|---|---|
committer | Robert Kern <rkern@enthought.com> | 2016-05-25 14:11:27 +0100 |
commit | 3991939341a25000c16171647e4547eaa6d86055 (patch) | |
tree | 0cede493e9daf5a4e4e7bd06344c65a9daad4bcb /numpy | |
parent | 75705e2e8d4139985ba61c0de170c10c85027efa (diff) | |
download | numpy-3991939341a25000c16171647e4547eaa6d86055.tar.gz |
BUG: fix handling of right edge of final bin.
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/lib/function_base.py | 6 | ||||
-rw-r--r-- | numpy/lib/tests/test_function_base.py | 5 |
2 files changed, 8 insertions, 3 deletions
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 24afa39c2..93f4f2634 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -667,14 +667,14 @@ def histogram(a, bins=10, range=None, normed=False, weights=None, # Compute the bin indices, and for values that lie exactly on mx we # need to subtract one indices = tmp_a.astype(np.intp) - equals_endpoint = (indices == bins) - indices[equals_endpoint] -= 1 + indices[indices == bins] -= 1 # The index computation is not guaranteed to give exactly # consistent results within ~1 ULP of the bin edges. decrement = tmp_a_data < bin_edges[indices] indices[decrement] -= 1 - increment = (tmp_a_data >= bin_edges[indices + 1]) & ~equals_endpoint + # The last bin includes the right edge. The other bins do not. + increment = (tmp_a_data >= bin_edges[indices + 1]) & (indices != bins - 1) indices[increment] += 1 # We now compute the histogram using bincount diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 868a28036..15fbbfbd7 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -1418,6 +1418,11 @@ class TestHistogram(TestCase): self.assertGreaterEqual(x, left) self.assertLess(x, right) + def test_last_bin_inclusive_range(self): + arr = np.array([0., 0., 0., 1., 2., 3., 3., 4., 5.]) + hist, edges = np.histogram(arr, bins=30, range=(-0.5, 5)) + self.assertEqual(hist[-1], 1) + class TestHistogramOptimBinNums(TestCase): """ |