Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Shifted numbering in type names in records.py module (Trac #372) #970

@numpy-gitbot

Description

@numpy-gitbot

Original ticket http://projects.scipy.org/numpy/ticket/372 on 2006-11-04 by @FrancescAlted, assigned to unknown.

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:

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions