summaryrefslogtreecommitdiff
path: root/numpy/lib/nanfunctions.py
diff options
context:
space:
mode:
authorEric Wieser <wieser.eric@gmail.com>2017-04-27 23:02:43 +0100
committerEric Wieser <wieser.eric@gmail.com>2017-04-29 03:13:19 +0100
commita77709c67051a1d19ed4caa83b0b7686ba22bfc4 (patch)
tree887f3d7e6bb671c8ef7449bf9af4f6cb598a7867 /numpy/lib/nanfunctions.py
parentc607456d9eac8743e5280adeec5496fd2621ae6a (diff)
downloadnumpy-a77709c67051a1d19ed4caa83b0b7686ba22bfc4.tar.gz
BUG: Fix incorrect behavior of nanfunctions on object arrays
Fixes gh-8974 and gh-9008
Diffstat (limited to 'numpy/lib/nanfunctions.py')
-rw-r--r--numpy/lib/nanfunctions.py24
1 files changed, 16 insertions, 8 deletions
diff --git a/numpy/lib/nanfunctions.py b/numpy/lib/nanfunctions.py
index d9df9563f..1e342b932 100644
--- a/numpy/lib/nanfunctions.py
+++ b/numpy/lib/nanfunctions.py
@@ -63,11 +63,17 @@ def _replace_nan(a, val):
"""
a = np.array(a, subok=True, copy=True)
- if not issubclass(a.dtype.type, np.inexact):
- return a, None
+ if a.dtype == np.object_:
+ # object arrays do not support `isnan` (gh-9009), so make a guess
+ mask = a != a
+ elif issubclass(a.dtype.type, np.inexact):
+ mask = np.isnan(a)
+ else:
+ mask = None
+
+ if mask is not None:
+ np.copyto(a, val, where=mask)
- mask = np.isnan(a)
- np.copyto(a, val, where=mask)
return a, mask
@@ -228,8 +234,9 @@ def nanmin(a, axis=None, out=None, keepdims=np._NoValue):
kwargs = {}
if keepdims is not np._NoValue:
kwargs['keepdims'] = keepdims
- if not isinstance(a, np.ndarray) or type(a) is np.ndarray:
- # Fast, but not safe for subclasses of ndarray
+ if type(a) is np.ndarray and a.dtype != np.object_:
+ # Fast, but not safe for subclasses of ndarray, or object arrays,
+ # which do not implement isnan (gh-9009), or fmin correctly (gh-8975)
res = np.fmin.reduce(a, axis=axis, out=out, **kwargs)
if np.isnan(res).any():
warnings.warn("All-NaN axis encountered", RuntimeWarning, stacklevel=2)
@@ -335,8 +342,9 @@ def nanmax(a, axis=None, out=None, keepdims=np._NoValue):
kwargs = {}
if keepdims is not np._NoValue:
kwargs['keepdims'] = keepdims
- if not isinstance(a, np.ndarray) or type(a) is np.ndarray:
- # Fast, but not safe for subclasses of ndarray
+ if type(a) is np.ndarray and a.dtype != np.object_:
+ # Fast, but not safe for subclasses of ndarray, or object arrays,
+ # which do not implement isnan (gh-9009), or fmax correctly (gh-8975)
res = np.fmax.reduce(a, axis=axis, out=out, **kwargs)
if np.isnan(res).any():
warnings.warn("All-NaN slice encountered", RuntimeWarning, stacklevel=2)