We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Wrong std for implicitly complex input
<!-- Please describe the issue in detail here, and fill in the fields below -->

Standart deviation formula could give unexpected result (e.g. imaginary values) when input is implicitly complex.

### Reproducing code example:

<!-- A short code example that reproduces the problem/missing feature. It should be
self-contained, i.e., possible to run as-is via 'python myproblem.py' -->

```python
import numpy as np
a = np.array([None, 0])
a[0] = 1j
b = np.array([1j, 0])

print(f'res={np.std(a)}, dtype={a.dtype}') # gives imaginary std
print(f'res={np.std(b)}, dtype={b.dtype}')

```

<!-- Remove these sections for a feature request -->

### Error message:

<!-- If you are reporting a segfault please include a GDB traceback, which you
can generate by following
https://github.com/numpy/numpy/blob/master/doc/source/dev/development_environment.rst#debugging -->

<!-- Full error message, if any (starting from line Traceback: ...) -->

### Numpy/Python version information:
numpy 1.16.2
python 3.7

<!-- Output from 'import sys, numpy; print(numpy.__version__, sys.version)' -->
res=0.5j, dtype=object
res=0.5, dtype=complex128


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `numpy/core/_methods.py`
Content:
```
1 """
2 Array methods which are called by both the C-code for the method
3 and the Python code for the NumPy-namespace function
4 
5 """
6 from __future__ import division, absolute_import, print_function
7 
8 import warnings
9 
10 from numpy.core import multiarray as mu
11 from numpy.core import umath as um
12 from numpy.core.numeric import asanyarray
13 from numpy.core import numerictypes as nt
14 from numpy._globals import _NoValue
15 
16 # save those O(100) nanoseconds!
17 umr_maximum = um.maximum.reduce
18 umr_minimum = um.minimum.reduce
19 umr_sum = um.add.reduce
20 umr_prod = um.multiply.reduce
21 umr_any = um.logical_or.reduce
22 umr_all = um.logical_and.reduce
23 
24 # avoid keyword arguments to speed up parsing, saves about 15%-20% for very
25 # small reductions
26 def _amax(a, axis=None, out=None, keepdims=False,
27           initial=_NoValue, where=True):
28     return umr_maximum(a, axis, None, out, keepdims, initial, where)
29 
30 def _amin(a, axis=None, out=None, keepdims=False,
31           initial=_NoValue, where=True):
32     return umr_minimum(a, axis, None, out, keepdims, initial, where)
33 
34 def _sum(a, axis=None, dtype=None, out=None, keepdims=False,
35          initial=_NoValue, where=True):
36     return umr_sum(a, axis, dtype, out, keepdims, initial, where)
37 
38 def _prod(a, axis=None, dtype=None, out=None, keepdims=False,
39           initial=_NoValue, where=True):
40     return umr_prod(a, axis, dtype, out, keepdims, initial, where)
41 
42 def _any(a, axis=None, dtype=None, out=None, keepdims=False):
43     return umr_any(a, axis, dtype, out, keepdims)
44 
45 def _all(a, axis=None, dtype=None, out=None, keepdims=False):
46     return umr_all(a, axis, dtype, out, keepdims)
47 
48 def _count_reduce_items(arr, axis):
49     if axis is None:
50         axis = tuple(range(arr.ndim))
51     if not isinstance(axis, tuple):
52         axis = (axis,)
53     items = 1
54     for ax in axis:
55         items *= arr.shape[ax]
56     return items
57 
58 def _mean(a, axis=None, dtype=None, out=None, keepdims=False):
59     arr = asanyarray(a)
60 
61     is_float16_result = False
62     rcount = _count_reduce_items(arr, axis)
63     # Make this warning show up first
64     if rcount == 0:
65         warnings.warn("Mean of empty slice.", RuntimeWarning, stacklevel=2)
66 
67     # Cast bool, unsigned int, and int to float64 by default
68     if dtype is None:
69         if issubclass(arr.dtype.type, (nt.integer, nt.bool_)):
70             dtype = mu.dtype('f8')
71         elif issubclass(arr.dtype.type, nt.float16):
72             dtype = mu.dtype('f4')
73             is_float16_result = True
74 
75     ret = umr_sum(arr, axis, dtype, out, keepdims)
76     if isinstance(ret, mu.ndarray):
77         ret = um.true_divide(
78                 ret, rcount, out=ret, casting='unsafe', subok=False)
79         if is_float16_result and out is None:
80             ret = arr.dtype.type(ret)
81     elif hasattr(ret, 'dtype'):
82         if is_float16_result:
83             ret = arr.dtype.type(ret / rcount)
84         else:
85             ret = ret.dtype.type(ret / rcount)
86     else:
87         ret = ret / rcount
88 
89     return ret
90 
91 def _var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False):
92     arr = asanyarray(a)
93 
94     rcount = _count_reduce_items(arr, axis)
95     # Make this warning show up on top.
96     if ddof >= rcount:
97         warnings.warn("Degrees of freedom <= 0 for slice", RuntimeWarning,
98                       stacklevel=2)
99 
100     # Cast bool, unsigned int, and int to float64 by default
101     if dtype is None and issubclass(arr.dtype.type, (nt.integer, nt.bool_)):
102         dtype = mu.dtype('f8')
103 
104     # Compute the mean.
105     # Note that if dtype is not of inexact type then arraymean will
106     # not be either.
107     arrmean = umr_sum(arr, axis, dtype, keepdims=True)
108     if isinstance(arrmean, mu.ndarray):
109         arrmean = um.true_divide(
110                 arrmean, rcount, out=arrmean, casting='unsafe', subok=False)
111     else:
112         arrmean = arrmean.dtype.type(arrmean / rcount)
113 
114     # Compute sum of squared deviations from mean
115     # Note that x may not be inexact and that we need it to be an array,
116     # not a scalar.
117     x = asanyarray(arr - arrmean)
118     if issubclass(arr.dtype.type, nt.complexfloating):
119         x = um.multiply(x, um.conjugate(x), out=x).real
120     else:
121         x = um.multiply(x, x, out=x)
122     ret = umr_sum(x, axis, dtype, out, keepdims)
123 
124     # Compute degrees of freedom and make sure it is not negative.
125     rcount = max([rcount - ddof, 0])
126 
127     # divide by degrees of freedom
128     if isinstance(ret, mu.ndarray):
129         ret = um.true_divide(
130                 ret, rcount, out=ret, casting='unsafe', subok=False)
131     elif hasattr(ret, 'dtype'):
132         ret = ret.dtype.type(ret / rcount)
133     else:
134         ret = ret / rcount
135 
136     return ret
137 
138 def _std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False):
139     ret = _var(a, axis=axis, dtype=dtype, out=out, ddof=ddof,
140                keepdims=keepdims)
141 
142     if isinstance(ret, mu.ndarray):
143         ret = um.sqrt(ret, out=ret)
144     elif hasattr(ret, 'dtype'):
145         ret = ret.dtype.type(um.sqrt(ret))
146     else:
147         ret = um.sqrt(ret)
148 
149     return ret
150 
151 def _ptp(a, axis=None, out=None, keepdims=False):
152     return um.subtract(
153         umr_maximum(a, axis, None, out, keepdims),
154         umr_minimum(a, axis, None, None, keepdims),
155         out
156     )
157
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/numpy/core/_methods.py b/numpy/core/_methods.py
--- a/numpy/core/_methods.py
+++ b/numpy/core/_methods.py
@@ -115,10 +115,11 @@
     # 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.

{"golden_diff": "diff --git a/numpy/core/_methods.py b/numpy/core/_methods.py\n--- a/numpy/core/_methods.py\n+++ b/numpy/core/_methods.py\n@@ -115,10 +115,11 @@\n     # Note that x may not be inexact and that we need it to be an array,\n     # not a scalar.\n     x = asanyarray(arr - arrmean)\n-    if issubclass(arr.dtype.type, nt.complexfloating):\n-        x = um.multiply(x, um.conjugate(x), out=x).real\n-    else:\n+    if issubclass(arr.dtype.type, (nt.floating, nt.integer)):\n         x = um.multiply(x, x, out=x)\n+    else:\n+        x = um.multiply(x, um.conjugate(x), out=x).real\n+\n     ret = umr_sum(x, axis, dtype, out, keepdims)\n \n     # Compute degrees of freedom and make sure it is not negative.\n", "issue": "Wrong std for implicitly complex input\n<!-- Please describe the issue in detail here, and fill in the fields below -->\r\n\r\nStandart deviation formula could give unexpected result (e.g. imaginary values) when input is implicitly complex.\r\n\r\n### Reproducing code example:\r\n\r\n<!-- A short code example that reproduces the problem/missing feature. It should be\r\nself-contained, i.e., possible to run as-is via 'python myproblem.py' -->\r\n\r\n```python\r\nimport numpy as np\r\na = np.array([None, 0])\r\na[0] = 1j\r\nb = np.array([1j, 0])\r\n\r\nprint(f'res={np.std(a)}, dtype={a.dtype}') # gives imaginary std\r\nprint(f'res={np.std(b)}, dtype={b.dtype}')\r\n\r\n```\r\n\r\n<!-- Remove these sections for a feature request -->\r\n\r\n### Error message:\r\n\r\n<!-- If you are reporting a segfault please include a GDB traceback, which you\r\ncan generate by following\r\nhttps://github.com/numpy/numpy/blob/master/doc/source/dev/development_environment.rst#debugging -->\r\n\r\n<!-- Full error message, if any (starting from line Traceback: ...) -->\r\n\r\n### Numpy/Python version information:\r\nnumpy 1.16.2\r\npython 3.7\r\n\r\n<!-- Output from 'import sys, numpy; print(numpy.__version__, sys.version)' -->\r\nres=0.5j, dtype=object\r\nres=0.5, dtype=complex128\r\n\n", "before_files": [{"content": "\"\"\"\nArray methods which are called by both the C-code for the method\nand the Python code for the NumPy-namespace function\n\n\"\"\"\nfrom __future__ import division, absolute_import, print_function\n\nimport warnings\n\nfrom numpy.core import multiarray as mu\nfrom numpy.core import umath as um\nfrom numpy.core.numeric import asanyarray\nfrom numpy.core import numerictypes as nt\nfrom numpy._globals import _NoValue\n\n# save those O(100) nanoseconds!\numr_maximum = um.maximum.reduce\numr_minimum = um.minimum.reduce\numr_sum = um.add.reduce\numr_prod = um.multiply.reduce\numr_any = um.logical_or.reduce\numr_all = um.logical_and.reduce\n\n# avoid keyword arguments to speed up parsing, saves about 15%-20% for very\n# small reductions\ndef _amax(a, axis=None, out=None, keepdims=False,\n          initial=_NoValue, where=True):\n    return umr_maximum(a, axis, None, out, keepdims, initial, where)\n\ndef _amin(a, axis=None, out=None, keepdims=False,\n          initial=_NoValue, where=True):\n    return umr_minimum(a, axis, None, out, keepdims, initial, where)\n\ndef _sum(a, axis=None, dtype=None, out=None, keepdims=False,\n         initial=_NoValue, where=True):\n    return umr_sum(a, axis, dtype, out, keepdims, initial, where)\n\ndef _prod(a, axis=None, dtype=None, out=None, keepdims=False,\n          initial=_NoValue, where=True):\n    return umr_prod(a, axis, dtype, out, keepdims, initial, where)\n\ndef _any(a, axis=None, dtype=None, out=None, keepdims=False):\n    return umr_any(a, axis, dtype, out, keepdims)\n\ndef _all(a, axis=None, dtype=None, out=None, keepdims=False):\n    return umr_all(a, axis, dtype, out, keepdims)\n\ndef _count_reduce_items(arr, axis):\n    if axis is None:\n        axis = tuple(range(arr.ndim))\n    if not isinstance(axis, tuple):\n        axis = (axis,)\n    items = 1\n    for ax in axis:\n        items *= arr.shape[ax]\n    return items\n\ndef _mean(a, axis=None, dtype=None, out=None, keepdims=False):\n    arr = asanyarray(a)\n\n    is_float16_result = False\n    rcount = _count_reduce_items(arr, axis)\n    # Make this warning show up first\n    if rcount == 0:\n        warnings.warn(\"Mean of empty slice.\", RuntimeWarning, stacklevel=2)\n\n    # Cast bool, unsigned int, and int to float64 by default\n    if dtype is None:\n        if issubclass(arr.dtype.type, (nt.integer, nt.bool_)):\n            dtype = mu.dtype('f8')\n        elif issubclass(arr.dtype.type, nt.float16):\n            dtype = mu.dtype('f4')\n            is_float16_result = True\n\n    ret = umr_sum(arr, axis, dtype, out, keepdims)\n    if isinstance(ret, mu.ndarray):\n        ret = um.true_divide(\n                ret, rcount, out=ret, casting='unsafe', subok=False)\n        if is_float16_result and out is None:\n            ret = arr.dtype.type(ret)\n    elif hasattr(ret, 'dtype'):\n        if is_float16_result:\n            ret = arr.dtype.type(ret / rcount)\n        else:\n            ret = ret.dtype.type(ret / rcount)\n    else:\n        ret = ret / rcount\n\n    return ret\n\ndef _var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False):\n    arr = asanyarray(a)\n\n    rcount = _count_reduce_items(arr, axis)\n    # Make this warning show up on top.\n    if ddof >= rcount:\n        warnings.warn(\"Degrees of freedom <= 0 for slice\", RuntimeWarning,\n                      stacklevel=2)\n\n    # Cast bool, unsigned int, and int to float64 by default\n    if dtype is None and issubclass(arr.dtype.type, (nt.integer, nt.bool_)):\n        dtype = mu.dtype('f8')\n\n    # Compute the mean.\n    # Note that if dtype is not of inexact type then arraymean will\n    # not be either.\n    arrmean = umr_sum(arr, axis, dtype, keepdims=True)\n    if isinstance(arrmean, mu.ndarray):\n        arrmean = um.true_divide(\n                arrmean, rcount, out=arrmean, casting='unsafe', subok=False)\n    else:\n        arrmean = arrmean.dtype.type(arrmean / rcount)\n\n    # Compute sum of squared deviations from mean\n    # Note that x may not be inexact and that we need it to be an array,\n    # not a scalar.\n    x = asanyarray(arr - arrmean)\n    if issubclass(arr.dtype.type, nt.complexfloating):\n        x = um.multiply(x, um.conjugate(x), out=x).real\n    else:\n        x = um.multiply(x, x, out=x)\n    ret = umr_sum(x, axis, dtype, out, keepdims)\n\n    # Compute degrees of freedom and make sure it is not negative.\n    rcount = max([rcount - ddof, 0])\n\n    # divide by degrees of freedom\n    if isinstance(ret, mu.ndarray):\n        ret = um.true_divide(\n                ret, rcount, out=ret, casting='unsafe', subok=False)\n    elif hasattr(ret, 'dtype'):\n        ret = ret.dtype.type(ret / rcount)\n    else:\n        ret = ret / rcount\n\n    return ret\n\ndef _std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False):\n    ret = _var(a, axis=axis, dtype=dtype, out=out, ddof=ddof,\n               keepdims=keepdims)\n\n    if isinstance(ret, mu.ndarray):\n        ret = um.sqrt(ret, out=ret)\n    elif hasattr(ret, 'dtype'):\n        ret = ret.dtype.type(um.sqrt(ret))\n    else:\n        ret = um.sqrt(ret)\n\n    return ret\n\ndef _ptp(a, axis=None, out=None, keepdims=False):\n    return um.subtract(\n        umr_maximum(a, axis, None, out, keepdims),\n        umr_minimum(a, axis, None, None, keepdims),\n        out\n    )\n", "path": "numpy/core/_methods.py"}], "after_files": [{"content": "\"\"\"\nArray methods which are called by both the C-code for the method\nand the Python code for the NumPy-namespace function\n\n\"\"\"\nfrom __future__ import division, absolute_import, print_function\n\nimport warnings\n\nfrom numpy.core import multiarray as mu\nfrom numpy.core import umath as um\nfrom numpy.core.numeric import asanyarray\nfrom numpy.core import numerictypes as nt\nfrom numpy._globals import _NoValue\n\n# save those O(100) nanoseconds!\numr_maximum = um.maximum.reduce\numr_minimum = um.minimum.reduce\numr_sum = um.add.reduce\numr_prod = um.multiply.reduce\numr_any = um.logical_or.reduce\numr_all = um.logical_and.reduce\n\n# avoid keyword arguments to speed up parsing, saves about 15%-20% for very\n# small reductions\ndef _amax(a, axis=None, out=None, keepdims=False,\n          initial=_NoValue, where=True):\n    return umr_maximum(a, axis, None, out, keepdims, initial, where)\n\ndef _amin(a, axis=None, out=None, keepdims=False,\n          initial=_NoValue, where=True):\n    return umr_minimum(a, axis, None, out, keepdims, initial, where)\n\ndef _sum(a, axis=None, dtype=None, out=None, keepdims=False,\n         initial=_NoValue, where=True):\n    return umr_sum(a, axis, dtype, out, keepdims, initial, where)\n\ndef _prod(a, axis=None, dtype=None, out=None, keepdims=False,\n          initial=_NoValue, where=True):\n    return umr_prod(a, axis, dtype, out, keepdims, initial, where)\n\ndef _any(a, axis=None, dtype=None, out=None, keepdims=False):\n    return umr_any(a, axis, dtype, out, keepdims)\n\ndef _all(a, axis=None, dtype=None, out=None, keepdims=False):\n    return umr_all(a, axis, dtype, out, keepdims)\n\ndef _count_reduce_items(arr, axis):\n    if axis is None:\n        axis = tuple(range(arr.ndim))\n    if not isinstance(axis, tuple):\n        axis = (axis,)\n    items = 1\n    for ax in axis:\n        items *= arr.shape[ax]\n    return items\n\ndef _mean(a, axis=None, dtype=None, out=None, keepdims=False):\n    arr = asanyarray(a)\n\n    is_float16_result = False\n    rcount = _count_reduce_items(arr, axis)\n    # Make this warning show up first\n    if rcount == 0:\n        warnings.warn(\"Mean of empty slice.\", RuntimeWarning, stacklevel=2)\n\n    # Cast bool, unsigned int, and int to float64 by default\n    if dtype is None:\n        if issubclass(arr.dtype.type, (nt.integer, nt.bool_)):\n            dtype = mu.dtype('f8')\n        elif issubclass(arr.dtype.type, nt.float16):\n            dtype = mu.dtype('f4')\n            is_float16_result = True\n\n    ret = umr_sum(arr, axis, dtype, out, keepdims)\n    if isinstance(ret, mu.ndarray):\n        ret = um.true_divide(\n                ret, rcount, out=ret, casting='unsafe', subok=False)\n        if is_float16_result and out is None:\n            ret = arr.dtype.type(ret)\n    elif hasattr(ret, 'dtype'):\n        if is_float16_result:\n            ret = arr.dtype.type(ret / rcount)\n        else:\n            ret = ret.dtype.type(ret / rcount)\n    else:\n        ret = ret / rcount\n\n    return ret\n\ndef _var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False):\n    arr = asanyarray(a)\n\n    rcount = _count_reduce_items(arr, axis)\n    # Make this warning show up on top.\n    if ddof >= rcount:\n        warnings.warn(\"Degrees of freedom <= 0 for slice\", RuntimeWarning,\n                      stacklevel=2)\n\n    # Cast bool, unsigned int, and int to float64 by default\n    if dtype is None and issubclass(arr.dtype.type, (nt.integer, nt.bool_)):\n        dtype = mu.dtype('f8')\n\n    # Compute the mean.\n    # Note that if dtype is not of inexact type then arraymean will\n    # not be either.\n    arrmean = umr_sum(arr, axis, dtype, keepdims=True)\n    if isinstance(arrmean, mu.ndarray):\n        arrmean = um.true_divide(\n                arrmean, rcount, out=arrmean, casting='unsafe', subok=False)\n    else:\n        arrmean = arrmean.dtype.type(arrmean / rcount)\n\n    # Compute sum of squared deviations from mean\n    # Note that x may not be inexact and that we need it to be an array,\n    # not a scalar.\n    x = asanyarray(arr - arrmean)\n    if issubclass(arr.dtype.type, (nt.floating, nt.integer)):\n        x = um.multiply(x, x, out=x)\n    else:\n        x = um.multiply(x, um.conjugate(x), out=x).real\n\n    ret = umr_sum(x, axis, dtype, out, keepdims)\n\n    # Compute degrees of freedom and make sure it is not negative.\n    rcount = max([rcount - ddof, 0])\n\n    # divide by degrees of freedom\n    if isinstance(ret, mu.ndarray):\n        ret = um.true_divide(\n                ret, rcount, out=ret, casting='unsafe', subok=False)\n    elif hasattr(ret, 'dtype'):\n        ret = ret.dtype.type(ret / rcount)\n    else:\n        ret = ret / rcount\n\n    return ret\n\ndef _std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False):\n    ret = _var(a, axis=axis, dtype=dtype, out=out, ddof=ddof,\n               keepdims=keepdims)\n\n    if isinstance(ret, mu.ndarray):\n        ret = um.sqrt(ret, out=ret)\n    elif hasattr(ret, 'dtype'):\n        ret = ret.dtype.type(um.sqrt(ret))\n    else:\n        ret = um.sqrt(ret)\n\n    return ret\n\ndef _ptp(a, axis=None, out=None, keepdims=False):\n    return um.subtract(\n        umr_maximum(a, axis, None, out, keepdims),\n        umr_minimum(a, axis, None, None, keepdims),\n        out\n    )\n", "path": "numpy/core/_methods.py"}]}