summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/src/multiarray/arraytypes.c.src20
-rw-r--r--numpy/core/src/multiarray/calculation.c11
-rw-r--r--numpy/core/tests/test_numeric.py8
3 files changed, 31 insertions, 8 deletions
diff --git a/numpy/core/src/multiarray/arraytypes.c.src b/numpy/core/src/multiarray/arraytypes.c.src
index e3236ed3f..685aba542 100644
--- a/numpy/core/src/multiarray/arraytypes.c.src
+++ b/numpy/core/src/multiarray/arraytypes.c.src
@@ -3538,6 +3538,7 @@ static void
/* NaNs result in no clipping, so optimize the case away */
if (@isnan@(max_val)) {
if (min == NULL) {
+ memmove(out, in, ni * sizeof(@type@));
return;
}
max = NULL;
@@ -3549,6 +3550,7 @@ static void
#if @isfloat@
if (@isnan@(min_val)) {
if (max == NULL) {
+ memmove(out, in, ni * sizeof(@type@));
return;
}
min = NULL;
@@ -3560,6 +3562,9 @@ static void
if (@lt@(in[i], min_val)) {
out[i] = min_val;
}
+ else {
+ out[i] = in[i];
+ }
}
}
else if (min == NULL) {
@@ -3567,6 +3572,9 @@ static void
if (@gt@(in[i], max_val)) {
out[i] = max_val;
}
+ else {
+ out[i] = in[i];
+ }
}
}
else {
@@ -3577,6 +3585,9 @@ static void
else if (@gt@(in[i], max_val)) {
out[i] = max_val;
}
+ else {
+ out[i] = in[i];
+ }
}
}
}
@@ -3609,6 +3620,9 @@ static void
if (PyArray_CLT(in[i],min_val)) {
out[i] = min_val;
}
+ else {
+ out[i] = in[i];
+ }
}
}
else if (min == NULL) {
@@ -3616,6 +3630,9 @@ static void
if (PyArray_CGT(in[i], max_val)) {
out[i] = max_val;
}
+ else {
+ out[i] = in[i];
+ }
}
}
else {
@@ -3626,6 +3643,9 @@ static void
else if (PyArray_CGT(in[i], max_val)) {
out[i] = max_val;
}
+ else {
+ out[i] = in[i];
+ }
}
}
}
diff --git a/numpy/core/src/multiarray/calculation.c b/numpy/core/src/multiarray/calculation.c
index 5563a2515..edcca9857 100644
--- a/numpy/core/src/multiarray/calculation.c
+++ b/numpy/core/src/multiarray/calculation.c
@@ -921,7 +921,9 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o
func = PyArray_DESCR(self)->f->fastclip;
if (func == NULL
|| (min != NULL && !PyArray_CheckAnyScalar(min))
- || (max != NULL && !PyArray_CheckAnyScalar(max))) {
+ || (max != NULL && !PyArray_CheckAnyScalar(max))
+ || PyArray_ISBYTESWAPPED(self)
+ || (out && PyArray_ISBYTESWAPPED(out))) {
return _slow_array_clip(self, min, max, out);
}
/* Use the fast scalar clip function */
@@ -1032,7 +1034,6 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o
}
}
-
/*
* Check to see if input is single-segment, aligned,
* and in native byteorder
@@ -1138,12 +1139,6 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o
"same shape as the input.");
goto fail;
}
- if (PyArray_DATA(newout) != PyArray_DATA(newin)) {
- if (PyArray_AssignArray(newout, newin,
- NULL, NPY_DEFAULT_ASSIGN_CASTING) < 0) {
- goto fail;
- }
- }
/* Now we can call the fast-clip function */
min_data = max_data = NULL;
diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py
index 7948b1355..b482bf230 100644
--- a/numpy/core/tests/test_numeric.py
+++ b/numpy/core/tests/test_numeric.py
@@ -1493,6 +1493,14 @@ class TestClip(TestCase):
assert_array_strict_equal(a2, ac)
self.assertTrue(a2 is a)
+ def test_clip_nan(self):
+ d = np.arange(7.)
+ assert_equal(d.clip(min=np.nan), d)
+ assert_equal(d.clip(max=np.nan), d)
+ assert_equal(d.clip(min=np.nan, max=np.nan), d)
+ assert_equal(d.clip(min=-2, max=np.nan), d)
+ assert_equal(d.clip(min=np.nan, max=10), d)
+
class TestAllclose(object):
rtol = 1e-5