From f297d317f9d2355228fb3a265e9ff3c69e37b1e2 Mon Sep 17 00:00:00 2001 From: Chirag Nighut Date: Mon, 1 Apr 2019 19:19:09 +0530 Subject: BUG: Fix of `var` method for complex object arrays Previously, an object array which contained complex numbers would give complex output, as it was simply multiplied with itself instead of with its conjugate. --- numpy/core/_methods.py | 7 ++++--- numpy/core/tests/test_numeric.py | 3 +++ 2 files changed, 7 insertions(+), 3 deletions(-) (limited to 'numpy/core') diff --git a/numpy/core/_methods.py b/numpy/core/_methods.py index 51362c761..953e7e1b8 100644 --- a/numpy/core/_methods.py +++ b/numpy/core/_methods.py @@ -115,10 +115,11 @@ def _var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False): # Note that x may not be inexact and that we need it to be an array, # not a scalar. x = asanyarray(arr - arrmean) - if issubclass(arr.dtype.type, nt.complexfloating): - x = um.multiply(x, um.conjugate(x), out=x).real - else: + if issubclass(arr.dtype.type, (nt.floating, nt.integer)): x = um.multiply(x, x, out=x) + else: + x = um.multiply(x, um.conjugate(x), out=x).real + ret = umr_sum(x, axis, dtype, out, keepdims) # Compute degrees of freedom and make sure it is not negative. diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py index 90ac43a56..1822a7adf 100644 --- a/numpy/core/tests/test_numeric.py +++ b/numpy/core/tests/test_numeric.py @@ -216,6 +216,9 @@ class TestNonarrayArgs(object): assert_(np.isnan(np.var([]))) assert_(w[0].category is RuntimeWarning) + B = np.array([None, 0]) + B[0] = 1j + assert_almost_equal(np.var(B), 0.25) class TestIsscalar(object): def test_isscalar(self): -- cgit v1.2.1