summaryrefslogtreecommitdiff
path: root/numpy/lib/tests/test_function_base.py
diff options
context:
space:
mode:
authorEthan Kruse <eakruse@uw.edu>2015-10-19 13:29:01 -0700
committerEthan Kruse <eakruse@uw.edu>2015-10-21 15:46:49 -0700
commit5caf4c932e43c47d73fad761e3257bb0d4551cc2 (patch)
tree963b87fbb2b55b64bc41e0148a6fbb1f9d5bde26 /numpy/lib/tests/test_function_base.py
parent17146b2a873e835630ad58d00b73e97ba1fdb214 (diff)
downloadnumpy-5caf4c932e43c47d73fad761e3257bb0d4551cc2.tar.gz
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.
Diffstat (limited to 'numpy/lib/tests/test_function_base.py')
-rw-r--r--numpy/lib/tests/test_function_base.py28
1 files changed, 28 insertions, 0 deletions
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)