<p dir="auto"><em>Original ticket <a href="http://projects.scipy.org/numpy/ticket/372" rel="nofollow">http://projects.scipy.org/numpy/ticket/372</a> on 2006-11-04 by <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/FrancescAlted/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/FrancescAlted">@FrancescAlted</a>, assigned to unknown.</em></p>
<p dir="auto">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:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="In [7]:numpy.array([(1,2), (3,4)], &quot;i4,i4&quot;)
Out[7]:
array([(1, 2), (3, 4)],
      dtype=[('f0', '&lt;i4'), ('f1', '&lt;i4')])
In [8]:numpy.rec.array([(1,2), (3,4)], &quot;i4,i4&quot;)
Out[8]:
recarray([(1, 2), (3, 4)],
      dtype=[('f0', '&lt;i4'), ('f1', '&lt;i4')])
In [9]:numpy.rec.array([(1,2), (3,4)])
Out[9]:
recarray([(1, 2), (3, 4)],
      dtype=[('f1', '&lt;i4'), ('f2', '&lt;i4')])                   # !
In [10]:numpy.rec.fromarrays([(1,2), (3,4)], &quot;i4,i4&quot;)
Out[10]:
recarray([(1, 3), (2, 4)],
      dtype=[('f0', '&lt;i4'), ('f1', '&lt;i4')])
In [11]:numpy.rec.fromarrays([(1,2), (3,4)])
Out[11]:
recarray([(1, 3), (2, 4)],
      dtype=[('f1', '&lt;i4'), ('f2', '&lt;i4')])                   # !"><pre class="notranslate"><code class="notranslate">In [7]:numpy.array([(1,2), (3,4)], "i4,i4")
Out[7]:
array([(1, 2), (3, 4)],
      dtype=[('f0', '&lt;i4'), ('f1', '&lt;i4')])
In [8]:numpy.rec.array([(1,2), (3,4)], "i4,i4")
Out[8]:
recarray([(1, 2), (3, 4)],
      dtype=[('f0', '&lt;i4'), ('f1', '&lt;i4')])
In [9]:numpy.rec.array([(1,2), (3,4)])
Out[9]:
recarray([(1, 2), (3, 4)],
      dtype=[('f1', '&lt;i4'), ('f2', '&lt;i4')])                   # !
In [10]:numpy.rec.fromarrays([(1,2), (3,4)], "i4,i4")
Out[10]:
recarray([(1, 3), (2, 4)],
      dtype=[('f0', '&lt;i4'), ('f1', '&lt;i4')])
In [11]:numpy.rec.fromarrays([(1,2), (3,4)])
Out[11]:
recarray([(1, 3), (2, 4)],
      dtype=[('f1', '&lt;i4'), ('f2', '&lt;i4')])                   # !
</code></pre></div>
<p dir="auto">The next patch is a cure for this:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="--- 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 &quot;f1, f2,...&quot;
-        # if not enough names are specified, they will be assigned as &quot;f[n+1],
-        # f[n+2],...&quot; etc. where n is the number of specified names...&quot;
-        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 &quot;f0, f1,...&quot;
+        # if not enough names are specified, they will be assigned as &quot;f[n],
+        # f[n+1],...&quot; etc. where n is the number of specified names...&quot;
+        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:"><pre class="notranslate"><code class="notranslate">--- 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:
</code></pre></div>
<h3 dir="auto">Reproducing code example:</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np
x = np.rec.array([('abcd',)], dtype=[('a', str)])
print(repr(x))"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-s1">x</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-s1">rec</span>.<span class="pl-en">array</span>([(<span class="pl-s">'abcd'</span>,)], <span class="pl-s1">dtype</span><span class="pl-c1">=</span>[(<span class="pl-s">'a'</span>, <span class="pl-s1">str</span>)])
<span class="pl-en">print</span>(<span class="pl-en">repr</span>(<span class="pl-s1">x</span>))</pre></div>
<h3 dir="auto">Erroneous Behavior:</h3>
<p dir="auto"><code class="notranslate">str</code> type is inferred as <code class="notranslate">&lt;U</code> dtype.</p>
<p dir="auto">A similar expression using numpy arrays infers the length for <code class="notranslate">'&lt;UN'</code> dtype.</p>
<h3 dir="auto">Numpy/Python version information:</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="1.15.4 3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)]"><pre class="notranslate"><code class="notranslate">1.15.4 3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)]
</code></pre></div>