Skip to content

record array: passing formats as tuple raises error (Trac #1139) #1737

Open

Description

@thouis

Original ticket http://projects.scipy.org/numpy/ticket/1139 on 2009-06-17 by @huard, assigned to unknown.

If a tuple is passed to the formats argument to rec.fromarrays for example, the following error is raised:

/usr/local/lib/python2.5/site-packages/numpy-1.4.0.dev7045-py2.5-linux-x86_64.egg/numpy/core/records.pyc in fromarrays(arrayList, dtype, shape, formats, names, titles, aligned, byteorder)
    506         _names = descr.names
    507     else:
--> 508         parsed = format_parser(formats, names, titles, aligned, byteorder)
    509         _names = parsed._names
    510         descr = parsed._descr

/usr/local/lib/python2.5/site-packages/numpy-1.4.0.dev7045-py2.5-linux-x86_64.egg/numpy/core/records.pyc in __init__(self, formats, names, titles, aligned, byteorder)
    110     """
    111     def __init__(self, formats, names, titles, aligned=False, byteorder=None):
--> 112         self._parseFormats(formats, aligned)
    113         self._setfieldnames(names, titles)
    114         self._createdescr(byteorder)

/usr/local/lib/python2.5/site-packages/numpy-1.4.0.dev7045-py2.5-linux-x86_64.egg/numpy/core/records.pyc in _parseFormats(self, formats, aligned)
    124                 formats.append('')
    125             formats = ','.join(formats)
--> 126         dtype = sb.dtype(formats, aligned)
    127         fields = dtype.fields
    128         if fields is None:

TypeError: data type not understood
WARNING: Failure executing file: <load.py>

If the tuple is converted into a list, then everything works fine.

Activity

  1. thouis commented on Oct 19, 2012

    @thouis
    ContributorAuthor

    @huard wrote on 2009-06-17

    Here is a patch for a smoke test:

    Index: numpy/core/tests/test_records.py
    ===================================================================
    --- numpy/core/tests/test_records.py    (revision 7045)
    +++ numpy/core/tests/test_records.py    (working copy)
    @@ -89,6 +89,20 @@
             assert_array_equal(ra['field'], [[5,5,5]])
             assert callable(ra.field)
    
    +class TestFromarrays(TestCase):
    +    def test_smoke(self):
    +        s = ['a','b','c']
    +        i = [1, 2, 3]
    +        f = [.1, .2, .3]
    +        
    +        
    +        rec = np.rec.fromarrays([s,i,f], names=['strings', 'ints', 'floats'], formats=['c', 'i4', 'f8'])
    +    
    +        # Regression test for ticket #1139. format_parser chokes on formats tuple.
    +        rec = np.rec.fromarrays([s,i,f], names=('strings', 'ints', 'floats'), formats=('c', 'i4', 'f8'))
    +        
    +
    +
     class TestRecord(TestCase):
         def setUp(self):
             self.data = np.rec.fromrecords([(1,2,3),(4,5,6)],
    
  2. thouis commented on Oct 19, 2012

    @thouis
    ContributorAuthor

    Milestone changed to 1.4.0 by @cournape on 2009-11-25

  3. thouis commented on Oct 19, 2012

    @thouis
    ContributorAuthor

    Milestone changed to NumPy 2.0 by @mwiebe on 2011-03-24

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions