8000 BUG: Allow nan values in the data when the bins are explicit · numpy/numpy@f0ffd58 · GitHub
[go: up one dir, main page]

Skip to content

Commit f0ffd58

Browse files
committed
BUG: Allow nan values in the data when the bins are explicit
Fixes gh-7503 Closes gh-8984
1 parent d45cf0b commit f0ffd58

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

doc/release/1.15.0-notes.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ to maintain compatibility, aliased at ``np.lib.function_base.histogram(dd)``.
6464
Code that does ``from np.lib.function_base import *`` will need to be updated
6565
with the new location, and should consider not using ``import *`` in future.
6666

67+
``histogram`` will accept NaN values when explicit bins are given
68+
-----------------------------------------------------------------
69+
Previously it would fail when trying to compute a finite range for the data.
70+
Since the range is ignored anyway when the bins are given explcitly, this error
71+
was needless.
72+
73+
Note that calling `histogram` on NaN values continues to raise the
74+
`RuntimeWarning`s typical of working with nan values, which can be silenced
75+
as usual with `errstate`.
76+
6777
``np.r_`` works with 0d arrays, and ``np.ma.mr_` works with ``np.ma.masked``
6878
----------------------------------------------------------------------------
6979
0d arrays passed to the `r_` and `mr_` concatenation helpers are now treated as

numpy/lib/histograms.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,6 @@ def _get_bin_edges(a, bins, range, weights):
259259
The upper bound, lowerbound, and number of bins, used in the optimized
260260
implementation of `histogram` that works on uniform bins.
261261
"""
262-
first_edge, last_edge = _get_outer_edges(a, range)
263-
264262
# parse the overloaded bins argument
265263
n_equal_bins = None
266264
bin_edges = None
@@ -276,6 +274,8 @@ def _get_bin_edges(a, bins, range, weights):
276274
raise TypeError("Automated estimation of the number of "
277275
"bins is not supported for weighted data")
278276

277+
first_edge, last_edge = _get_outer_edges(a, range)
278+
279279
# truncate the range if needed
280280
if range is not None:
281281
keep = (a >= first_edge)
@@ -304,6 +304,8 @@ def _get_bin_edges(a, bins, range, weights):
304304
if n_equal_bins < 1:
305305
raise ValueError('`bins` must be positive, when an integer')
306306

307+
first_edge, last_edge = _get_outer_edges(a, range)
308+
307309
elif np.ndim(bins) == 1:
308310
bin_edges = np.asarray(bins)
309311
if np.any(bin_edges[:-1] > bin_edges[1:]):

numpy/lib/tests/test_histograms.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,31 @@ def test_object_array_of_0d(self):
249249
np.histogram([np.array([0.5]) for i in range(10)] + [.500000000000001])
250250
np.histogram([np.array([0.5]) for i in range(10)] + [.5])
251251

252+
def test_some_nan_values(self):
253+
# gh-7503
254+
one_nan = np.array([0, 1, np.nan])
255+
all_nan = np.array([np.nan, np.nan])
256+
257+
# the internal commparisons with NaN give warnings
258+
sup = suppress_warnings()
259+
sup.filter(RuntimeWarning)
260+
with sup:
261+
# can't infer range with nan
262+
assert_raises(ValueError, histogram, one_nan, bins='auto')
263+
assert_raises(ValueError, histogram, all_nan, bins='auto')
264+
265+
# explicit range solves the problem
266+
h, b = histogram(one_nan, bins='auto', range=(0, 1))
267+
assert_equal(h.sum(), 2) # nan is not counted
268+
h, b = histogram(all_nan, bins='auto', range=(0, 1))
269+
assert_equal(h.sum(), 0) # nan is not counted
270+
271+
# as does an explicit set of bins
272+
h, b = histogram(one_nan, bins=[0, 1])
273+
assert_equal(h.sum(), 2) # nan is not counted
274+
h, b = histogram(all_nan, bins=[0, 1])
275+
assert_equal(h.sum(), 0) # nan is not counted
276+
252277

253278
class TestHistogramOptimBinNums(object):
254279
"""

0 commit comments

Comments
 (0)
0