Skip to content
Browse files

BUG: core: make .std() and .var() respect the out= keyword (#1434)

(cherry picked from commit r8505)
  • Loading branch information...
1 parent 54d2f18 commit e9ae7f13b2fdeb2b554720a16af0e817b5019890 @pv pv committed Jul 24, 2010
Showing with 21 additions and 3 deletions.
  1. +8 −3 numpy/core/src/multiarray/calculation.c
  2. +13 −0 numpy/core/tests/test_regression.py
View
11 numpy/core/src/multiarray/calculation.c
@@ -393,18 +393,23 @@ __New_PyArray_Std(PyArrayObject *self, int axis, int rtype, PyArrayObject *out,
ret = PyArray_GenericUnaryFunction((PyAO *)obj1, n_ops.sqrt);
Py_DECREF(obj1);
}
- if (ret == NULL || PyArray_CheckExact(self)) {
- return ret;
+ if (ret == NULL) {
+ return NULL;
+ }
+ if (PyArray_CheckExact(self)) {
+ goto finish;
}
if (PyArray_Check(self) && Py_TYPE(self) == Py_TYPE(ret)) {
- return ret;
+ goto finish;
}
obj1 = PyArray_EnsureArray(ret);
if (obj1 == NULL) {
return NULL;
}
ret = PyArray_View((PyAO *)obj1, NULL, Py_TYPE(self));
Py_DECREF(obj1);
+
+finish:
if (out) {
if (PyArray_CopyAnyInto(out, (PyArrayObject *)ret) < 0) {
Py_DECREF(ret);
View
13 numpy/core/tests/test_regression.py
@@ -1324,5 +1324,18 @@ def test_ticket_1538(self):
assert_equal(type(getattr(x, name)), np.float32,
err_msg=name)
+ def test_ticket_1434(self):
+ # Check that the out= argument in var and std has an effect
+ data = np.array(((1,2,3),(4,5,6),(7,8,9)))
+ out = np.zeros((3,))
+
+ ret = data.var(axis=1, out=out)
+ assert_(ret is out)
+ assert_array_equal(ret, data.var(axis=1))
+
+ ret = data.std(axis=1, out=out)
+ assert_(ret is out)
+ assert_array_equal(ret, data.std(axis=1))
+
if __name__ == "__main__":
run_module_suite()

0 comments on commit e9ae7f1

Please sign in to comment.
Something went wrong with that request. Please try again.