diff options
author | Joe Kington <joferkington@gmail.com> | 2012-03-04 14:18:27 -0600 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2012-03-04 15:22:54 -0700 |
commit | b9f0f1f8b54d0e4cf74ea5e7d80893d66585c4a1 (patch) | |
tree | 370d57b07a38d6ceb5c5a4bc071a5f7c317da948 | |
parent | 07e640c634ae7e7f859b3def5783e2e50b6002f4 (diff) | |
download | numpy-b9f0f1f8b54d0e4cf74ea5e7d80893d66585c4a1.tar.gz |
ENH: Made "isclose()" NA-aware, added release note and versionadded.
-rw-r--r-- | doc/release/2.0.0-notes.rst | 7 | ||||
-rw-r--r-- | numpy/core/numeric.py | 9 | ||||
-rw-r--r-- | numpy/core/tests/test_numeric.py | 11 |
3 files changed, 24 insertions, 3 deletions
diff --git a/doc/release/2.0.0-notes.rst b/doc/release/2.0.0-notes.rst index defdc9c00..7c2bfa8a5 100644 --- a/doc/release/2.0.0-notes.rst +++ b/doc/release/2.0.0-notes.rst @@ -94,6 +94,13 @@ A generic sampling function has been added which will generate samples from a given array-like. The samples can be with or without replacement, and with uniform or given non-uniform probabilities. +New function isclose +-------------------- + +Returns a boolean array where two arrays are element-wise equal within a +tolerance. Both relative and absolute tolerance can be specified. The +function is NA aware. + Preliminary multi-dimensional support in the polynomial package --------------------------------------------------------------- diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index e4693431c..846429016 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -2059,6 +2059,8 @@ def isclose(a, b, rtol=1.e-5, atol=1.e-8, equal_nan=False): Notes ----- + .. versionadded:: 1.7.0 + For finite values, isclose uses the following equation to test whether two floating point values are equivalent. @@ -2090,17 +2092,18 @@ def isclose(a, b, rtol=1.e-5, atol=1.e-8, equal_nan=False): y = array(b, copy=False, subok=True, ndmin=1) xfin = isfinite(x) yfin = isfinite(y) - if all(xfin) and all(yfin): + if all(xfin, skipna=True) and all(yfin, skipna=True): return within_tol(x, y, atol, rtol) else: finite = xfin & yfin + cond = zeros_like(finite, subok=True, maskna=finite.flags.maskna) # Because we're using boolean indexing, x & y must be the same shape. # Ideally, we'd just do x, y = broadcast_arrays(x, y). It's in # lib.stride_tricks, though, so we can't import it here. - cond = zeros_like(finite, subok=True) x = x * ones_like(cond) y = y * ones_like(cond) - # Avoid subtraction with infinite/nan values... + # Avoid subtraction with infinite/nan values and indexing with na... + finite[isna(finite)] = False cond[finite] = within_tol(x[finite], y[finite], atol, rtol) # Check for equality of infinite values... cond[~finite] = (x[~finite] == y[~finite]) diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py index 174a12c31..5233d0f88 100644 --- a/numpy/core/tests/test_numeric.py +++ b/numpy/core/tests/test_numeric.py @@ -1322,6 +1322,17 @@ class TestIsclose(object): # Ensure that the mask isn't modified... assert_array_equal([True, True, False], y.mask) + def test_maskna_arrays(self): + x = array([NA, 1, 2, 3]) + y = array([0, 1, 2, NA]) + assert_array_equal(isclose(x, y), array([NA, True, True, NA])) + + assert_array_equal(isclose(NA, arange(3)), array([NA, NA, NA])) + + x = array([NA, nan, 2, 3]) + y = array([nan, 1, 2, NA]) + assert_array_equal(isclose(x, y), array([NA, False, True, NA])) + def test_scalar_return(self): assert_(isscalar(isclose(1, 1))) |