Note that there are some explanatory texts on larger screens.

plurals
  1. POnumpy append_field gives shape error for new field with 2d shape
    primarykey
    data
    text
    <p>I have a structured numpy array, I want to use the recfunctions library <a href="http://pyopengl.sourceforge.net/pydoc/numpy.lib.recfunctions.html" rel="nofollow noreferrer">http://pyopengl.sourceforge.net/pydoc/numpy.lib.recfunctions.html</a> function append_fields() or rec_append_fields() to append a field with some shape to it. However, I get an error:</p> <blockquote> <p>ValueError: operands could not be broadcast together with shapes (10) (10,3) </p> </blockquote> <p>where <code>10</code> is the length of my existing array, and <code>(3,)</code> is the shape of the field I want to append.</p> <p>For example:</p> <pre><code>import numpy as np from numpy.lib.recfunctions import append_fields my_structured_array = np.array( zip([0,1,2,3],[[4.3,3.2],[1.4,5.6],[6.,2.5],[4.5,5.4]]), dtype=[('id','int8'),('pos','2float16')] ) my_new_field = np.ones( len(my_structured_array), dtype='2int8' ) my_appended_array = append_fields( my_structured_array, 'new', data=my_new_field ) </code></pre> <blockquote> <p>ValueError: operands could not be broadcast together with shapes (4) (4,2) </p> </blockquote> <p>Any ideas? I tried making <code>my_new_field</code> a list of tuples and putting a dtype argument with the proper shape into the append_fields():</p> <pre><code>my_new_field = len(my_structured_array)*[(1,1)] my_appended_array = append_fields( my_structured_array, 'new', data=my_new_field, dtype='2int8' ) </code></pre> <p>but that seems to end up the same once it gets converted to a numpy array.</p> <p>None of this seems to change when I use rec_append_fields() instead of simply append_fields()</p> <p>EDIT: In light of the fact that my new field doesn't have the same shape as my array, I suppose that my desired append is impossible, suggested by <a href="https://stackoverflow.com/users/895755/radicalbiscuit">@radicalbiscuit</a>.</p> <pre><code>In : my_new_field.shape Out: (4, 2) In : my_structured_array.shape Out: (4,) </code></pre> <p>But, I included one of the original fields in the array with shape different from the original array to make my point, which is that a field does not have to have the same shape as the structured array. How can I append a field like this?</p> <pre><code>In : my_structured_array['pos'].shape Out: (4, 2) In : my_new_field.shape Out: (4, 2) </code></pre> <p>I should note that for my application, I can append an empty field as long as it's possible to somehow change the shape later. Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload