numpy - python structured/recarray type conversion behaviour -


i'm confused behaviour of type conversion when constructing structured/recarray:

this simple example takes in numerical fields defines type string:

data = [(1.0, 2), (3.0, 4)] np.array(data, dtype=[('x', str), ('y', int)]) 

which produces:

array([('', 2), ('', 4)], dtype=[('x', 's'), ('y', '<i8')]) 

so values converted empty strings not expect from:

str(1.0) 

which produces string '1.0'. what's causing behaviour?

you need specify string width, e.g. 'a3':

>>> np.array([(1.0, 2),(3.0,4)],dtype=[('x','a3'),('y',int)]) array([('1.0', 2), ('3.0', 4)],        dtype=[('x', 's3'), ('y', '<i4')]) 

just using str means string field of 0 bytes - of course small hold string rendition of float.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -