Keeping strings when converting a python list to a layered structured array

I have a data structure that looks like this:

data = [ ('a', 1.0, 2.0), ('b', 2.0, 4.0), ('c', 3.0, 6.0) ] 

I want to convert it to a structured array using numpy. However, when I try to do the following, I save the floats, but I lose the string information:

 import numpy x = numpy.array(data, dtype=[('label', str), ('x', float), ('y', float)]) print x 

Result:

 >>> [('', 1.0, 2.0) ('', 2.0, 4.0) ('', 3.0, 6.0)] 

Can someone explain why this is happening, and how can I store string information?

+4
source share
1 answer

You can see the problem if you print the array and carefully look:

 >>> numpy.array(data, dtype=[('label', str), ('x', float), ('y', float)]) array([('', 1.0, 2.0), ('', 2.0, 4.0), ('', 3.0, 6.0)], dtype=[('label', '|S0'), ('x', '<f8'), ('y', '<f8')]) 

The first field has a data type of '|S0' - a row field of zero width. Make the string field longer - here is the string field:

 >>> numpy.array(data, dtype=[('label', 'S2'), ('x', float), ('y', float)]) array([('a', 1.0, 2.0), ('b', 2.0, 4.0), ('c', 3.0, 6.0)], dtype=[('label', '|S2'), ('x', '<f8'), ('y', '<f8')]) 

You can also do this as described here :

 >>> numpy.array(data, dtype=[('label', (str, 2)), ('x', float), ('y', float)]) array([('a', 1.0, 2.0), ('b', 2.0, 4.0), ('c', 3.0, 6.0)], dtype=[('label', '|S2'), ('x', '<f8'), ('y', '<f8')]) 
+4
source

Source: https://habr.com/ru/post/1439551/


All Articles