From 0d25dc4175e00cdaf9545e8b1b1a5b879cf67248 Mon Sep 17 00:00:00 2001 From: Ethan Kruse Date: Mon, 19 Oct 2015 13:29:01 -0700 Subject: [PATCH 1/2] Potential fix for #6462 --- numpy/lib/function_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 555d083..fef69df 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -3339,7 +3339,7 @@ def _median(a, axis=None, out=None, overwrite_input=False): indexer[axis] = slice(index-1, index+1) # Check if the array contains any nan's - if np.issubdtype(a.dtype, np.inexact): + if np.issubdtype(a.dtype, np.inexact) and sz > 0: # warn and return nans like mean would rout = mean(part[indexer], axis=axis, out=out) part = np.rollaxis(part, axis, part.ndim) From 59d859fb2160950ac93267d7461ad952145c8724 Mon Sep 17 00:00:00 2001 From: Ethan Kruse Date: Tue, 20 Oct 2015 11:40:49 -0700 Subject: [PATCH 2/2] Added tests for median of empty arrays --- numpy/lib/tests/test_function_base.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 4516c92..aa41c1f 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -2597,6 +2597,36 @@ def test_nan_behavior(self): assert_equal(np.median(a, (0, 2)), b) assert_equal(len(w), 1) + def test_empty(self): + # empty arrays + a = np.array([], dtype=float) + with warnings.catch_warnings(record=True) as w: + warnings.filterwarnings('always', '', RuntimeWarning) + assert_equal(np.median(a), np.nan) + assert_(w[0].category is RuntimeWarning) + + # multiple dimensions + a = np.array([], dtype=float, ndmin=3) + # no axis + with warnings.catch_warnings(record=True) as w: + warnings.filterwarnings('always', '', RuntimeWarning) + assert_equal(np.median(a), np.nan) + assert_(w[0].category is RuntimeWarning) + + # axis 0 and 1 + b = np.array([], dtype=float, ndmin=2) + with warnings.catch_warnings(record=True) as w: + warnings.filterwarnings('always', '', RuntimeWarning) + assert_equal(np.median(a, axis=0), b) + assert_equal(np.median(a, axis=1), b) + + # axis 2 + b = np.array(np.nan, dtype=float, ndmin=2) + with warnings.catch_warnings(record=True) as w: + warnings.filterwarnings('always', '', RuntimeWarning) + assert_equal(np.median(a, axis=2), b) + assert_(w[0].category is RuntimeWarning) + def test_object(self): o = np.arange(7.) assert_(type(np.median(o.astype(object))), float)