diff --git a/numba/np/arraymath.py b/numba/np/arraymath.py index 33460a32dda..d6d3a70b10d 100644 --- a/numba/np/arraymath.py +++ b/numba/np/arraymath.py @@ -1590,13 +1590,16 @@ def np_median(a): if not isinstance(a, types.Array): return + is_datetime = as_dtype(a.dtype).char in 'mM' + def median_impl(a): # np.median() works on the flattened array, and we need a temporary # workspace anyway temp_arry = a.flatten() n = temp_arry.shape[0] + if not is_datetime and n == 0: + return np.nan return _median_inner(temp_arry, n) - return median_impl diff --git a/numba/tests/test_array_reductions.py b/numba/tests/test_array_reductions.py index d1ca334c2d5..36b8445693e 100644 --- a/numba/tests/test_array_reductions.py +++ b/numba/tests/test_array_reductions.py @@ -307,6 +307,10 @@ def check(arr): got = cfunc(arr) self.assertPreciseEqual(got, expected) + # check for empty array + a = np.array([]) + check(a) + # Odd sizes def check_odd(a): check(a)