Skip to content

flatten_dtype does not handle titles (Trac #1591) #2187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
numpy-gitbot opened this issue Oct 19, 2012 · 3 comments
Closed

flatten_dtype does not handle titles (Trac #1591) #2187

numpy-gitbot opened this issue Oct 19, 2012 · 3 comments

Comments

@numpy-gitbot
Copy link

Original ticket http://projects.scipy.org/numpy/ticket/1591 on 2010-08-23 by trac user nasturtium86, assigned to unknown.

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]

@numpy-gitbot
Copy link
Author

@pierregm wrote on 2010-09-13

  • Should be fixed in r8713. Please confirm so that I can close the ticket.

@numpy-gitbot
Copy link
Author

@rgommers wrote on 2010-10-14

>>> import numpy
>>> dtype2 = numpy.dtype([(("Amps","Current"),"f8"),(("Volts","Voltage"),"f8"),(("Watts","Power"),"f8")])
>>> import numpy.lib.npyio
>>> print(numpy.lib.npyio.flatten_dtype(dtype2))
[dtype('float64'), dtype('float64'), dtype('float64')]

@numpy-gitbot
Copy link
Author

Milestone changed to 1.6.0 by @mwiebe on 2011-05-24

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant