<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="In [11]: a = np.dtype({'names':['time','data','pad'], 'formats':['&lt;f8',('&lt;f8', (3, 2)),'&lt;f8'], 'offsets':[0,8,56], 'itemsize':64, 'aligned':True})

In [12]: b = np.zeros(10, dtype=a)

In [13]: b[3][1] = np.ones((3, 2))

In [14]: b
Out[14]:
array([(0.0, [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0.0),
       (0.0, [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0.0),
       (0.0, [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0.0),
       (0.0, [[1.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0.0),
       (0.0, [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0.0),
       (0.0, [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0.0),
       (0.0, [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0.0),
       (0.0, [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0.0),
       (0.0, [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0.0),
       (0.0, [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0.0)],
      dtype={'names':['time','data','pad'], 'formats':['&lt;f8',('&lt;f8', (3, 2)),'&lt;f8'], 'offsets':[0,8,56], 'itemsize':64, 'aligned':True})

In [15]: b[3][1][:] = np.ones((3, 2))"><pre class="notranslate"><span class="pl-v">In</span> [<span class="pl-c1">11</span>]: <span class="pl-s1">a</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">dtype</span>({<span class="pl-s">'names'</span>:[<span class="pl-s">'time'</span>,<span class="pl-s">'data'</span>,<span class="pl-s">'pad'</span>], <span class="pl-s">'formats'</span>:[<span class="pl-s">'&lt;f8'</span>,(<span class="pl-s">'&lt;f8'</span>, (<span class="pl-c1">3</span>, <span class="pl-c1">2</span>)),<span class="pl-s">'&lt;f8'</span>], <span class="pl-s">'offsets'</span>:[<span class="pl-c1">0</span>,<span class="pl-c1">8</span>,<span class="pl-c1">56</span>], <span class="pl-s">'itemsize'</span>:<span class="pl-c1">64</span>, <span class="pl-s">'aligned'</span>:<span class="pl-c1">True</span>})

<span class="pl-v">In</span> [<span class="pl-c1">12</span>]: <span class="pl-s1">b</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">zeros</span>(<span class="pl-c1">10</span>, <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s1">a</span>)

<span class="pl-v">In</span> [<span class="pl-c1">13</span>]: <span class="pl-s1">b</span>[<span class="pl-c1">3</span>][<span class="pl-c1">1</span>] <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">ones</span>((<span class="pl-c1">3</span>, <span class="pl-c1">2</span>))

<span class="pl-v">In</span> [<span class="pl-c1">14</span>]: <span class="pl-s1">b</span>
<span class="pl-v">Out</span>[<span class="pl-c1">14</span>]:
<span class="pl-en">array</span>([(<span class="pl-c1">0.0</span>, [[<span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>], [<span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>], [<span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>]], <span class="pl-c1">0.0</span>),
       (<span class="pl-c1">0.0</span>, [[<span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>], [<span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>], [<span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>]], <span class="pl-c1">0.0</span>),
       (<span class="pl-c1">0.0</span>, [[<span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>], [<span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>], [<span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>]], <span class="pl-c1">0.0</span>),
       (<span class="pl-c1">0.0</span>, [[<span class="pl-c1">1.0</span>, <span class="pl-c1">0.0</span>], [<span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>], [<span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>]], <span class="pl-c1">0.0</span>),
       (<span class="pl-c1">0.0</span>, [[<span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>], [<span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>], [<span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>]], <span class="pl-c1">0.0</span>),
       (<span class="pl-c1">0.0</span>, [[<span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>], [<span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>], [<span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>]], <span class="pl-c1">0.0</span>),
       (<span class="pl-c1">0.0</span>, [[<span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>], [<span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>], [<span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>]], <span class="pl-c1">0.0</span>),
       (<span class="pl-c1">0.0</span>, [[<span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>], [<span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>], [<span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>]], <span class="pl-c1">0.0</span>),
       (<span class="pl-c1">0.0</span>, [[<span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>], [<span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>], [<span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>]], <span class="pl-c1">0.0</span>),
       (<span class="pl-c1">0.0</span>, [[<span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>], [<span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>], [<span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>]], <span class="pl-c1">0.0</span>)],
      <span class="pl-s1">dtype</span><span class="pl-c1">=</span>{<span class="pl-s">'names'</span>:[<span class="pl-s">'time'</span>,<span class="pl-s">'data'</span>,<span class="pl-s">'pad'</span>], <span class="pl-s">'formats'</span>:[<span class="pl-s">'&lt;f8'</span>,(<span class="pl-s">'&lt;f8'</span>, (<span class="pl-c1">3</span>, <span class="pl-c1">2</span>)),<span class="pl-s">'&lt;f8'</span>], <span class="pl-s">'offsets'</span>:[<span class="pl-c1">0</span>,<span class="pl-c1">8</span>,<span class="pl-c1">56</span>], <span class="pl-s">'itemsize'</span>:<span class="pl-c1">64</span>, <span class="pl-s">'aligned'</span>:<span class="pl-c1">True</span>})

<span class="pl-v">In</span> [<span class="pl-c1">15</span>]: <span class="pl-s1">b</span>[<span class="pl-c1">3</span>][<span class="pl-c1">1</span>][:] <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">ones</span>((<span class="pl-c1">3</span>, <span class="pl-c1">2</span>))</pre></div>
<p dir="auto">But this works:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="In [16]: b
Out[16]:
array([(0.0, [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0.0),
       (0.0, [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0.0),
       (0.0, [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0.0),
       (0.0, [[1.0, 1.0], [1.0, 1.0], [1.0, 1.0]], 0.0),
       (0.0, [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0.0),
       (0.0, [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0.0),
       (0.0, [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0.0),
       (0.0, [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0.0),
       (0.0, [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0.0),
       (0.0, [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0.0)],
      dtype={'names':['time','data','pad'], 'formats':['&lt;f8',('&lt;f8', (3, 2)),'&lt;f8'], 'offsets':[0,8,56], 'itemsize':64, 'aligned':True})"><pre class="notranslate"><code class="notranslate">In [16]: b
Out[16]:
array([(0.0, [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0.0),
       (0.0, [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0.0),
       (0.0, [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0.0),
       (0.0, [[1.0, 1.0], [1.0, 1.0], [1.0, 1.0]], 0.0),
       (0.0, [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0.0),
       (0.0, [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0.0),
       (0.0, [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0.0),
       (0.0, [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0.0),
       (0.0, [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0.0),
       (0.0, [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0.0)],
      dtype={'names':['time','data','pad'], 'formats':['&lt;f8',('&lt;f8', (3, 2)),'&lt;f8'], 'offsets':[0,8,56], 'itemsize':64, 'aligned':True})
</code></pre></div>
<p dir="auto"><em>Original ticket <a href="http://projects.scipy.org/numpy/ticket/2006" rel="nofollow">http://projects.scipy.org/numpy/ticket/2006</a> on 2011-12-23 by trac user kevin000, assigned to unknown.</em></p>
<p dir="auto">Using a recarray with member assignment fails if any column uses the object data type.  I don't understand why this fails, especially since the dictionary style assignment works.</p>
<h1 dir="auto">Brief example showing what recarray assignments succeed or fail</h1>
<p dir="auto">import numpy as np</p>
<h1 dir="auto">recarray with integer datatypes</h1>
<p dir="auto">dt = np.dtype([('foo', 'i8'), ('bar', 'i8')])<br>
r = np.zeros((1,3), dtype=dt).view(np.recarray)<br>
r['foo'] = np.array([1, 2, 3])      # OK<br>
r.foo = np.array([1, 2, 3])         # OK</p>
<h1 dir="auto">recarray with an object datatype</h1>
<p dir="auto">dt = np.dtype([('foo', 'i8'), ('bar', 'O8')])<br>
r = np.zeros((1,3), dtype=dt).view(np.recarray)<br>
r['foo'] = np.array([1, 2, 3])      # OK<br>
r.foo = np.array([1, 2, 3])         # RuntimeError</p>
<p dir="auto">For the relevant C code search for "cannot call setfield on an object array" in this file:</p>
<p dir="auto"><a href="https://github.com/numpy/numpy/blob/master/numpy/core/src/multiarray/methods.c">https://github.com/numpy/numpy/blob/master/numpy/core/src/multiarray/methods.c</a></p>
<p dir="auto">Thanks!</p>
<p dir="auto">Kevin</p>