Permalink
Browse files

BUG bn.slow.replace()

  • Loading branch information...
1 parent 53498af commit d99fe4faea60c9bc9708d65c0d4ba2598b67516d @kwgoodman committed May 24, 2012
Showing with 22 additions and 2 deletions.
  1. +2 −2 README.rst
  2. +3 −0 bottleneck/slow/func.py
  3. +17 −0 bottleneck/tests/replace_test.py
View
4 README.rst
@@ -283,6 +283,6 @@ After you have installed Bottleneck, run the suite of unit tests::
>>> import bottleneck as bn
>>> bn.test()
<snip>
- Ran 90 tests in 31.197s
+ Ran 91 tests in 31.197s
OK
- <nose.result.TextTestResult run=90 errors=0 failures=0>
+ <nose.result.TextTestResult run=91 errors=0 failures=0>
View
3 bottleneck/slow/func.py
@@ -175,6 +175,9 @@ def replace(arr, old, new):
if type(arr) is not np.ndarray:
raise TypeError("`arr` must be a numpy array.")
if not issubclass(arr.dtype.type, np.inexact):
+ if old != old:
+ # int arrays do not contain NaN
+ return
if int(old) != old:
raise ValueError("Cannot safely cast `old` to int.")
if int(new) != new:
View
17 bottleneck/tests/replace_test.py
@@ -93,3 +93,20 @@ def test_non_array():
assert_raises(TypeError, bn.replace, a, 0, 1)
a = (1, 2, 3)
assert_raises(TypeError, bn.replace, a, 0, 1)
+
+# ---------------------------------------------------------------------------
+# Make sure bn.replace and bn.slow.replace can handle int arrays where
+# user wants to replace nans
+
+def test_replace_nan_int():
+ "Test replace, int array, old=nan, new=0"
+ a = np.arange(2*3*4).reshape(2,3,4)
+ actual = a.copy()
+ bn.replace(actual, np.nan, 0)
+ desired = a.copy()
+ msg = 'replace failed on int input looking for nans'
+ assert_array_equal(actual, desired, err_msg=msg)
+ actual = a.copy()
+ bn.slow.replace(actual, np.nan, 0)
+ msg = 'slow.replace failed on int input looking for nans'
+ assert_array_equal(actual, desired, err_msg=msg)

0 comments on commit d99fe4f

Please sign in to comment.