Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a numpy array of dtype objects to dtype complex

Tags:

python

numpy

I have a numpy array which I want to convert from an object to complex. If I take that array as dtype string and convert it, there is no problem:

In[22]: bane
Out[22]: array(['1.000027337501943-7.331085223659654E-6j',
       '1.0023086995640738-1.8228368353755985E-4j',
       '-0.017014515914781394-0.2820013864855318j'], 
       dtype='|S41')

In [23]: bane.astype(dtype=complex)
Out[23]: 
array([ 1.00002734 -7.33108522e-06j,  1.00230870 -1.82283684e-04j,
       -0.01701452 -2.82001386e-01j])

But when it is dtype object and I try to convert it, I get an error that a float is required. Why is this?

In [24]: bane.astype(dtype=object)
Out[24]: 
array(['1.000027337501943-7.331085223659654E-6j',
       '1.0023086995640738-1.8228368353755985E-4j',
       '-0.017014515914781394-0.2820013864855318j'], dtype=object)

In [25]: _.astype(dtype=complex)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-25-f5d89c8cc46c> in <module>()
----> 1 _.astype(dtype=complex)

TypeError: a float is required    

To convert it, I use two calls to the astype method which seems clumsy:

bane_obj
Out[27]: 
array(['1.000027337501943-7.331085223659654E-6j',
       '1.0023086995640738-1.8228368353755985E-4j',
       '-0.017014515914781394-0.2820013864855318j'], dtype=object)

In [28]: bane_obj.astype(dtype=str).astype(dtype=complex)
Out[28]: 
array([ 1.00002734 -7.33108522e-06j,  1.00230870 -1.82283684e-04j,
       -0.01701452 -2.82001386e-01j])
like image 576
mataleo99 Avatar asked Mar 13 '15 21:03

mataleo99


People also ask

How do I change the Dtype of a numpy array?

We have a method called astype(data_type) to change the data type of a numpy array. If we have a numpy array of type float64, then we can change it to int32 by giving the data type to the astype() method of numpy array. We can check the type of numpy array using the dtype class.

What is Dtype complex?

A data type object (an instance of numpy. dtype class) describes how the bytes in the fixed-size block of memory corresponding to an array item should be interpreted. It describes the following aspects of the data: Type of the data (integer, float, Python object, etc.)

Can numpy array have different data types?

Can an array store different data types? Yes, a numpy array can store different data String, Integer, Complex, Float, Boolean.


1 Answers

I think you might want to do the following, depending on what's inside the object type and if there's no padding to worry about:

bane.view(np.complex64) or
bane.view(np.complex128)

However if that does not work, which it didn't for some small tuple example I tried, the following worked:

bane.astype(np.float).view(np.complex64)

Consider using numpy structures rather than objects for the base dtype, you may have an easier time over all.

like image 93
Jason Newton Avatar answered Sep 20 '22 00:09

Jason Newton