From f91f4bcd050299c930092390b54ce9ba51fd70e0 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Sat, 22 May 2021 15:48:23 +0200 Subject: BUG: Fixed an issue wherein `nanmedian` could return an array with the wrong dtype --- numpy/lib/nanfunctions.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'numpy/lib/nanfunctions.py') diff --git a/numpy/lib/nanfunctions.py b/numpy/lib/nanfunctions.py index a02ad779f..02ad01a98 100644 --- a/numpy/lib/nanfunctions.py +++ b/numpy/lib/nanfunctions.py @@ -965,7 +965,9 @@ def _nanmedian1d(arr1d, overwrite_input=False): arr1d, overwrite_input = _remove_nan_1d(arr1d, overwrite_input=overwrite_input) if arr1d.size == 0: - return np.nan + # Ensure that a nan-esque scalar of the appropiate type (and unit) + # is returned for `timedelta64` and `complexfloating` + return np.array(np.nan).astype(arr1d.dtype, copy=False)[()] return np.median(arr1d, overwrite_input=overwrite_input) -- cgit v1.2.1 From a8b825c0379972234a86f30b76ae9fc853a88b5e Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Sat, 22 May 2021 16:04:26 +0200 Subject: BUG: Fixed an issue wherein `_nanmedian_small` would fail for `timedelta64`-based dtypes --- numpy/lib/nanfunctions.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'numpy/lib/nanfunctions.py') diff --git a/numpy/lib/nanfunctions.py b/numpy/lib/nanfunctions.py index 02ad01a98..719c529c1 100644 --- a/numpy/lib/nanfunctions.py +++ b/numpy/lib/nanfunctions.py @@ -1010,10 +1010,12 @@ def _nanmedian_small(a, axis=None, out=None, overwrite_input=False): for i in range(np.count_nonzero(m.mask.ravel())): warnings.warn("All-NaN slice encountered", RuntimeWarning, stacklevel=4) + + fill_value = np.timedelta64("NaT") if m.dtype.kind == "m" else np.nan if out is not None: - out[...] = m.filled(np.nan) + out[...] = m.filled(fill_value) return out - return m.filled(np.nan) + return m.filled(fill_value) def _nanmedian_dispatcher( -- cgit v1.2.1 From ae9314eff5d539122bf87800a1bc50a9f99762a8 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Sat, 22 May 2021 16:18:47 +0200 Subject: MAINT: Directly grab `nan` from the input array Directly grab a nan-esque object from the input array, rather than constructing a new one from scratch --- numpy/lib/nanfunctions.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'numpy/lib/nanfunctions.py') diff --git a/numpy/lib/nanfunctions.py b/numpy/lib/nanfunctions.py index 719c529c1..2c2c3435b 100644 --- a/numpy/lib/nanfunctions.py +++ b/numpy/lib/nanfunctions.py @@ -962,14 +962,16 @@ def _nanmedian1d(arr1d, overwrite_input=False): Private function for rank 1 arrays. Compute the median ignoring NaNs. See nanmedian for parameter usage """ - arr1d, overwrite_input = _remove_nan_1d(arr1d, - overwrite_input=overwrite_input) - if arr1d.size == 0: + arr1d_parsed, overwrite_input = _remove_nan_1d( + arr1d, overwrite_input=overwrite_input, + ) + + if arr1d_parsed.size == 0: # Ensure that a nan-esque scalar of the appropiate type (and unit) # is returned for `timedelta64` and `complexfloating` - return np.array(np.nan).astype(arr1d.dtype, copy=False)[()] + return arr1d[-1] - return np.median(arr1d, overwrite_input=overwrite_input) + return np.median(arr1d_parsed, overwrite_input=overwrite_input) def _nanmedian(a, axis=None, out=None, overwrite_input=False): -- cgit v1.2.1