diff options
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/lib/function_base.py | 22 | ||||
-rw-r--r-- | numpy/lib/tests/test_function_base.py | 4 |
2 files changed, 20 insertions, 6 deletions
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index f76bf2a78..3d7428e69 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -1435,15 +1435,25 @@ def _nanop(op, fill, a, axis=None): Processed data. """ - y = array(a,subok=True) + y = array(a, subok=True) mask = isnan(a) - if mask.all(): - return np.nan - if not issubclass(y.dtype.type, np.integer): - y[mask] = fill + # We only need to take care of NaN's in floating point arrays + if not np.issubdtype(y.dtype, int): + y[mask] = fill - return op(y, axis=axis) + res = op(y, axis=axis) + mask_all_along_axis = mask.all(axis=axis) + + # Along some axes, only nan's were encountered. As such, any values + # calculated along that axis should be set to nan. + if mask_all_along_axis.any(): + if np.isscalar(res): + res = np.nan + else: + res[mask_all_along_axis] = np.nan + + return res def nansum(a, axis=None): """ diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index daaa3e28e..6b7a1efc2 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -754,6 +754,10 @@ class TestNaNFuncts(TestCase): [ 0.59541557, 0.87964135, 0.70543747], [ 0.91084584, 0.84386844, 0.37068164]])) + def test_nanmin_allnan_on_axis(self): + assert_equal(isnan(nanmin([[nan]*2]*3, axis=1)), + [True, True, True]) + class TestCorrCoef(TestCase): def test_simple(self): |