4

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 into a structured array using numpy. However, when I try the following, I keep the floats but I lose the string information:

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

Resulting in:

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

Could anyone explain why this happens, and how I might keep the string information?

1
  • x = numpy.array(data, dtype=[('label', (str,1)), ('x', float), ('y', float)]) Commented Oct 13, 2012 at 14:29

1 Answer 1

4

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

>>> 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 zero width string field. Make the string field longer -- here's a 2-char 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 it this way, as documented 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')])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.