[Numpy-tickets] [NumPy] #372: Shifted numbering in type names in records.py module

NumPy numpy-tickets at scipy.net
Sat Nov 4 04:26:54 CST 2006


#372: Shifted numbering in type names in records.py module
--------------------+-------------------------------------------------------
 Reporter:  faltet  |       Owner:  somebody
     Type:  defect  |      Status:  new     
 Priority:  low     |   Milestone:          
Component:  Other   |     Version:  devel   
 Severity:  minor   |    Keywords:          
--------------------+-------------------------------------------------------
 When you create recarrays with records.py and not specify the dtype
 argument explicitely, the names are generated with 1 as base (i.e. 'f1',
 'f2', ...) instead of using 0 (i.e. 'f0','f1',...) which is the default in
 !NumPy. The next exposes the issue:

 {{{
 In [7]:numpy.array([(1,2), (3,4)], "i4,i4")
 Out[7]:
 array([(1, 2), (3, 4)],
       dtype=[('f0', '<i4'), ('f1', '<i4')])
 In [8]:numpy.rec.array([(1,2), (3,4)], "i4,i4")
 Out[8]:
 recarray([(1, 2), (3, 4)],
       dtype=[('f0', '<i4'), ('f1', '<i4')])
 In [9]:numpy.rec.array([(1,2), (3,4)])
 Out[9]:
 recarray([(1, 2), (3, 4)],
       dtype=[('f1', '<i4'), ('f2', '<i4')])                   # !
 In [10]:numpy.rec.fromarrays([(1,2), (3,4)], "i4,i4")
 Out[10]:
 recarray([(1, 3), (2, 4)],
       dtype=[('f0', '<i4'), ('f1', '<i4')])
 In [11]:numpy.rec.fromarrays([(1,2), (3,4)])
 Out[11]:
 recarray([(1, 3), (2, 4)],
       dtype=[('f1', '<i4'), ('f2', '<i4')])                   # !

 }}}

 The next patch is a cure for this:

 {{{
 --- numpy/core/records.py       (revision 3428)
 +++ numpy/core/records.py       (working copy)
 @@ -82,11 +82,11 @@
          else:
              self._names = []

 -        # if the names are not specified, they will be assigned as "f1,
 f2,..."
 -        # if not enough names are specified, they will be assigned as
 "f[n+1],
 -        # f[n+2],..." etc. where n is the number of specified names..."
 -        self._names += ['f%d' % i for i in range(len(self._names)+1,
 -                                                 self._nfields+1)]
 +        # if the names are not specified, they will be assigned as "f0,
 f1,..."
 +        # if not enough names are specified, they will be assigned as
 "f[n],
 +        # f[n+1],..." etc. where n is the number of specified names..."
 +        self._names += ['f%d' % i for i in range(len(self._names),
 +                                                 self._nfields)]
          # check for redundant names
          _dup = find_duplicate(self._names)
          if _dup:

 }}}

-- 
Ticket URL: <http://projects.scipy.org/scipy/numpy/ticket/372>
NumPy <http://projects.scipy.org/scipy/numpy>
The fundamental package needed for scientific computing with Python.


More information about the Numpy-tickets mailing list