diff options
author | Matti Picus <matti.picus@gmail.com> | 2018-10-10 22:59:52 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-10 22:59:52 +0300 |
commit | 41d2428a277c868a3160489e9ebe1a7bcbbf5fce (patch) | |
tree | b5ee3c4aad3f6802fb00622e70d0426099d70558 /numpy/lib | |
parent | 9942b71a6782a850cf59462a3a9ceec39e741880 (diff) | |
parent | e4e897fa1740ee4efaa276457debda9b8707b457 (diff) | |
download | numpy-41d2428a277c868a3160489e9ebe1a7bcbbf5fce.tar.gz |
Merge pull request #12108 from AetherUnbound/bugfix/12107
BUG: Allow boolean subtract in histogram
Diffstat (limited to 'numpy/lib')
-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 f03f30fb0..6a66e4a73 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 |