diff options
author | Matthew Bowden <bowdenm@spu.edu> | 2018-10-10 10:07:03 -0700 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2018-10-16 14:26:20 -0600 |
commit | 12aad091da0548f986d7a701fe7f431b6781f173 (patch) | |
tree | 678b9f3b8641901c0388c8b0b29b822c37abcc5a | |
parent | ab298c2cbd474211c0306f97033135a087b9e0f4 (diff) | |
download | numpy-12aad091da0548f986d7a701fe7f431b6781f173.tar.gz |
BUG: Allow boolean subtract in histogram
Convert bool to uint at start, rather than attempt a XOR
Only check type against np.bool_
Refactor warnings check
-rw-r--r-- | numpy/lib/histograms.py | 8 | ||||
-rw-r--r-- | numpy/lib/tests/test_histograms.py | 17 |
2 files changed, 25 insertions, 0 deletions
diff --git a/numpy/lib/histograms.py b/numpy/lib/histograms.py index 47ee92928..209b55867 100644 --- a/numpy/lib/histograms.py +++ b/numpy/lib/histograms.py @@ -220,6 +220,14 @@ _hist_bin_selectors = {'auto': _hist_bin_auto, def _ravel_and_check_weights(a, weights): """ Check a and weights have matching shapes, and ravel both """ a = np.asarray(a) + + # Ensure that the array is a "subtractable" dtype + if a.dtype == np.bool_: + warnings.warn("Converting input from {} to {} for compatibility." + .format(a.dtype, np.uint8), + RuntimeWarning, stacklevel=2) + a = a.astype(np.uint8) + if weights is not None: weights = np.asarray(weights) if weights.shape != a.shape: diff --git a/numpy/lib/tests/test_histograms.py b/numpy/lib/tests/test_histograms.py index 561f5f938..a71060a46 100644 --- a/numpy/lib/tests/test_histograms.py +++ b/numpy/lib/tests/test_histograms.py @@ -141,6 +141,23 @@ class TestHistogram(object): counts_hist, xedges, yedges = np.histogram2d(x, y, bins=100) assert_equal(counts_hist.sum(), 3.) + def test_bool_conversion(self): + # gh-12107 + # Reference integer histogram + a = np.array([1, 1, 0], dtype=np.uint8) + int_hist, int_edges = np.histogram(a) + + # Should raise an warning on booleans + # Ensure that the histograms are equivalent, need to suppress + # the warnings to get the actual outputs + with suppress_warnings() as sup: + rec = sup.record(RuntimeWarning, 'Converting input from .*') + hist, edges = np.histogram([True, True, False]) + # A warning should be issued + assert_equal(len(rec), 1) + assert_array_equal(hist, int_hist) + assert_array_equal(edges, int_edges) + def test_weights(self): v = np.random.rand(100) w = np.ones(100) * 5 |