[NumPy-Tickets] [NumPy] #1591: flatten_dtype does not handle titles

NumPy Trac numpy-tickets@scipy....
Mon Aug 23 11:08:19 CDT 2010


#1591: flatten_dtype does not handle titles
-------------------------------------+--------------------------------------
 Reporter:  nasturtium86             |       Owner:  somebody
     Type:  defect                   |      Status:  new     
 Priority:  normal                   |   Milestone:  2.0.0   
Component:  numpy.lib                |     Version:  1.3.0   
 Keywords:  flatten_dtype titles io  |  
-------------------------------------+--------------------------------------
 numpy.lib.io.flatten_dtype() raises "ValueError: too many values to
 unpack" when you run it on a dtype that has titles.

 For example, this code works:

 {{{
 import numpy
 dtype1 = numpy.dtype([("Current","f8"),("Voltage","f8"), "Power","f8")])
 print numpy.lib.io.flatten_dtype(dtype1)
 }}}

 But this code doesn't:

 {{{
 import numpy
 dtype2 =
 numpy.dtype([(("Amps","Current"),"f8"),(("Volts","Voltage"),"f8"),(("Watts","Power"),"f8")])
 print numpy.lib.io.flatten_dtype(dtype2)
 }}}

 The following monkey patch fixes the problem:

 {{{
 def flatten_dtype(ndtype):
     """
     Unpack a structured data-type.

     """
     names = ndtype.names
     if names is None:
         return [ndtype]
     else:
         types = []
         for field in names:
             typ_fields = ndtype.fields[field]
             flat_dt = flatten_dtype(typ_fields[0])
             types.extend(flat_dt)
         return types
 numpy.lib.io.flatten_dtype=flatten_dtype
 }}}

 With this monkey patch in place, both dtype1 and dtype2 are handled
 correctly.

 There is a thread open about this issue at [http://www.mail-archive.com
 /numpy-discussion@scipy.org/msg27441.html]

-- 
Ticket URL: <http://projects.scipy.org/numpy/ticket/1591>
NumPy <http://projects.scipy.org/numpy>
My example project


More information about the NumPy-Tickets mailing list