From f0ffd58e7dbe8f4b111b0c090e8df0c3b779e1fe Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Sat, 23 Dec 2017 11:47:26 +0000 Subject: BUG: Allow nan values in the data when the bins are explicit Fixes gh-7503 Closes gh-8984 --- numpy/lib/tests/test_histograms.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'numpy/lib/tests') diff --git a/numpy/lib/tests/test_histograms.py b/numpy/lib/tests/test_histograms.py index 8319041b8..0986ad16b 100644 --- a/numpy/lib/tests/test_histograms.py +++ b/numpy/lib/tests/test_histograms.py @@ -249,6 +249,31 @@ class TestHistogram(object): np.histogram([np.array([0.5]) for i in range(10)] + [.500000000000001]) np.histogram([np.array([0.5]) for i in range(10)] + [.5]) + def test_some_nan_values(self): + # gh-7503 + one_nan = np.array([0, 1, np.nan]) + all_nan = np.array([np.nan, np.nan]) + + # the internal commparisons with NaN give warnings + sup = suppress_warnings() + sup.filter(RuntimeWarning) + with sup: + # can't infer range with nan + assert_raises(ValueError, histogram, one_nan, bins='auto') + assert_raises(ValueError, histogram, all_nan, bins='auto') + + # explicit range solves the problem + h, b = histogram(one_nan, bins='auto', range=(0, 1)) + assert_equal(h.sum(), 2) # nan is not counted + h, b = histogram(all_nan, bins='auto', range=(0, 1)) + assert_equal(h.sum(), 0) # nan is not counted + + # as does an explicit set of bins + h, b = histogram(one_nan, bins=[0, 1]) + assert_equal(h.sum(), 2) # nan is not counted + h, b = histogram(all_nan, bins=[0, 1]) + assert_equal(h.sum(), 0) # nan is not counted + class TestHistogramOptimBinNums(object): """ -- cgit v1.2.1