1

I have two numpy structured arrays arr1, arr2.
arr1 has fields ['f1','f2','f3'].
arr2 has fields ['f1','f2','f3','f4'].
I.e.:

arr1 = [[f1_1_1,  f2_1_1,  f3_1_1 ],    arr2 = [[f1_2_1,  f2_2_1,  f3_2_1,  f4_2_1 ],
        [f1_1_2,  f2_1_2,  f3_1_2 ],            [f1_2_2,  f2_2_2,  f3_2_2,  f4_2_2 ],
        ...                        ,            ...                                 ,
        [f1_1_N1, f2_1_N1, f3_1_N1]]            [f1_2_N2, f2_2_N2, f3_2_N2, f4_2_N2]]

I want to assign various slices of arr1 to the corresponding slice of arr2 (slices in the indexes and in the fields). See below for the various cases.

From answers I found (to related, but not exactly the same, questions) it seemed to me that the only way to do it is assigning one slice at a time, for a single field, i.e., something like

arr2['f1'][0:1] = arr1['f1'][0:1]

(and I can confirm this works), looping over all source fields in the slice.

Is there a way to assign all intended source fields in the slice at a time?


I mean to assign, say, the elements x in the image

Case 1 (only some fields in arr1)

arr1 = [[  x   ,    x   ,  f3_1_1 ],    arr2 = [[  x   ,    x   ,  f3_2_1,  f4_2_1 ],
        [  x   ,    x   ,  f3_1_2 ],            [  x   ,    x   ,  f3_2_2,  f4_2_2 ],
        ...                        ,            ...                                 ,
        [f1_1_N1, f2_1_N1, f3_1_N1]]            [f1_2_N2, f2_2_N2, f3_2_N2, f4_2_N2]]

Case 2 (all fields in arr1)

arr1 = [[  x   ,    x   ,    x    ],    arr2 = [[  x   ,    x   ,    x   ,  f4_2_1 ],
        [  x   ,    x   ,    x    ],            [  x   ,    x   ,    x   ,  f4_2_2 ],
        ...                        ,            ...                                 ,
        [f1_1_N1, f2_1_N1, f3_1_N1]]            [f1_2_N2, f2_2_N2, f3_2_N2, f4_2_N2]]

Case 3
arr1 has fields ['f1','f2','f3','f5'].
arr2 has fields ['f1','f2','f3','f4'].
Assign a slice of ['f1','f2','f3']


Sources:

Python Numpy Structured Array (recarray) assigning values into slices

Convert a slice of a structured array to regular NumPy array in NumPy 1.14

Joe
  • 6,758
  • 2
  • 26
  • 47

1 Answers1

1

You can do it for example like that:

import numpy as np

x = np.array([('Rex', 9, 81.0), ('Fido', 3, 27.0)], dtype=[('name', 'U10'), ('age', 'i4'), ('weight', 'f4')])
y = np.array([('Carl', 10, 75.0), ('Joe', 7, 76.0)], dtype=[('name2', 'U10'), ('age2', 'i4'), ('weight', 'f4')])

print(x[['name', 'age']])
print(y[['name2', 'age2']])

# multiple field indexing
y[['name2', 'age2']] = x[['name', 'age']]

print(y[['name2', 'age2']])

# you can also use slicing if you want specific parts or the size does not match
y[:1][['name2', 'age2']] = x[1:][['name', 'age']]

print(y[:][['name2', 'age2']])

The names field names can be different, I am not sure about the dtypes and if there is (down)casting.

https://docs.scipy.org/doc/numpy/user/basics.rec.html#assignment-from-other-structured-arrays

https://docs.scipy.org/doc/numpy/user/basics.rec.html#accessing-multiple-fields

Joe
  • 6,758
  • 2
  • 26
  • 47
  • The `[:]` aren't necessary. You should demonstrate how it works with multiple field indexing. – hpaulj Dec 08 '19 at 16:31