OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

numpy: how to fill multiple fields in a structured array at once

  • Thread starter Thread starter Federico Barabas
  • Start date Start date
F

Federico Barabas

Guest
Very simple question: I have a structured array with multiple columns and I'd like to fill only some of them (but more than one) with another preexisting array.

This is what I'm trying:

Code:
strc = np.zeros(4, dtype=[('x', int), ('y', int), ('z', int)])
x = np.array([2, 3])
strc[['x', 'y']][0] = x

This gives me this future warning:

main:1: FutureWarning: Numpy has detected that you (may be) writing to an array returned by numpy.diagonal or by selecting multiple fields in a record array. This code will likely break in a future numpy release -- see numpy.diagonal or arrays.indexing reference docs for details. The quick fix is to make an explicit copy (e.g., do arr.diagonal().copy() or arr[['f0','f1']].copy()).

But even though this is a warning, the structured array doesn't get filled. So far I'm iterating over both arrays and it works but I guess that's highly inefficient. Is there a better way?
<p>Very simple question: I have a structured array with multiple columns and I'd like to fill only some of them (but more than one) with another preexisting array.</p>

<p>This is what I'm trying:</p>

<pre><code>strc = np.zeros(4, dtype=[('x', int), ('y', int), ('z', int)])
x = np.array([2, 3])
strc[['x', 'y']][0] = x
</code></pre>

<p>This gives me this future warning:</p>

<blockquote>
<p><strong>main</strong>:1: FutureWarning: Numpy has detected that you (may be) writing to an array
returned by numpy.diagonal or by selecting multiple fields in a record
array. This code will likely break in a future numpy release --
see numpy.diagonal or arrays.indexing reference docs for details.
The quick fix is to make an explicit copy (e.g., do
arr.diagonal().copy() or arr[['f0','f1']].copy()).</p>
</blockquote>

<p>But even though this is a warning, the structured array doesn't get filled. So far I'm iterating over both arrays and it works but I guess that's highly inefficient. Is there a better way?</p>
 

Latest posts

D
Replies
0
Views
1
Domantas Giedraitis
D
Top