I have an numpy-array with a float data type, but an extern function needs it with a complex data type. When I use a.view(np.complex)
, the real values are also used for the complex values, messing up my further calculations, i.e.:
a = [1, 2, 3]
b = a.view(np.complex)
> b = [1+1i, 2+2i, 3+3i]
Is there a command such that I get
> b = [1+0i, 2+0i, 3+0i]
?
Yes, use astype()
:
In [6]: b = a.astype(complex)
In [7]: b
Out[7]: array([ 1.+0.j, 2.+0.j, 3.+0.j])
In [8]: b.dtype
Out[8]: dtype('complex128')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With