From 5caf4c932e43c47d73fad761e3257bb0d4551cc2 Mon Sep 17 00:00:00 2001 From: Ethan Kruse Date: Mon, 19 Oct 2015 13:29:01 -0700 Subject: BUG: Make median work for empty arrays (issue #6462) np.median([]) returns NaN. Fixes bug/regression that raised an IndexError. Added tests to ensure continued support of empty arrays. --- numpy/lib/tests/test_function_base.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'numpy/lib/tests/test_function_base.py') diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 4516c9248..cc53c2b8e 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -2597,6 +2597,34 @@ class TestMedian(TestCase): 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) + 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) -- cgit v1.2.1