summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2016-05-25 09:37:38 -0600
committerCharles Harris <charlesr.harris@gmail.com>2016-05-25 09:37:38 -0600
commit27e85997067da402a2c2e64b50290bdc0ef4cb48 (patch)
tree0cede493e9daf5a4e4e7bd06344c65a9daad4bcb
parent75705e2e8d4139985ba61c0de170c10c85027efa (diff)
parent3991939341a25000c16171647e4547eaa6d86055 (diff)
downloadnumpy-27e85997067da402a2c2e64b50290bdc0ef4cb48.tar.gz
Merge pull request #7675 from rkern/fix/histogram-endpoint
BUG: fix handling of right edge of final bin.
-rw-r--r--numpy/lib/function_base.py6
-rw-r--r--numpy/lib/tests/test_function_base.py5
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):
"""