diff options
author | Warren Weckesser <warren.weckesser@gmail.com> | 2019-08-27 15:15:26 -0400 |
---|---|---|
committer | Warren Weckesser <warren.weckesser@gmail.com> | 2019-10-15 17:42:58 -0400 |
commit | 3ff4924ead45ef6db81778daae08e3c939ea4629 (patch) | |
tree | 9e8c4c9726d1fbd8be32a75ce85a0314ac4c812d /numpy/lib/tests | |
parent | dc20ec8c857bc1f1b717b56f3a5c64dbf31f16ac (diff) | |
download | numpy-3ff4924ead45ef6db81778daae08e3c939ea4629.tar.gz |
BUG: lib: Fix histogram problem with signed integer arrays.
An input such as
np.histogram(np.array([-2, 0, 127], dtype=np.int8), bins="auto")
would raise the exception
ValueError: Number of samples, -1, must be non-negative.
The problem was that the peak-to-peak value for the input array was
computed with the `ptp` method, which returned negative values for
signed integer arrays when the actual value was more than the
maximum signed value of the array's data type.
The fix is to use a peak-to-peak function that returns an
unsigned value for signed integer arrays.
Closes gh-14379.
Diffstat (limited to 'numpy/lib/tests')
-rw-r--r-- | numpy/lib/tests/test_histograms.py | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/numpy/lib/tests/test_histograms.py b/numpy/lib/tests/test_histograms.py index 4895a722c..dbf189f3e 100644 --- a/numpy/lib/tests/test_histograms.py +++ b/numpy/lib/tests/test_histograms.py @@ -8,6 +8,7 @@ from numpy.testing import ( assert_array_almost_equal, assert_raises, assert_allclose, assert_array_max_ulp, assert_raises_regex, suppress_warnings, ) +import pytest class TestHistogram(object): @@ -591,6 +592,16 @@ class TestHistogramOptimBinNums(object): msg += " with datasize of {0}".format(testlen) assert_equal(len(a), numbins, err_msg=msg) + @pytest.mark.parametrize("bins", ['auto', 'fd', 'doane', 'scott', + 'stone', 'rice', 'sturges']) + def test_signed_integer_data(self, bins): + # Regression test for gh-14379. + a = np.array([-2, 0, 127], dtype=np.int8) + hist, edges = np.histogram(a, bins=bins) + hist32, edges32 = np.histogram(a.astype(np.int32), bins=bins) + assert_array_equal(hist, hist32) + assert_array_equal(edges, edges32) + def test_simple_weighted(self): """ Check that weighted data raises a TypeError |