[SciPy-User] Combining arrays based on (x,y) index values

Robert Kern robert.kern@gmail....
Sat Mar 6 16:30:20 CST 2010

```On Sat, Mar 6, 2010 at 15:46, Vincent Davis <vincent@vincentdavis.net> wrote:
>
> I have 3 arrays, each have an x and y column and I need to extend each row with the additional rows from the other arrays. Only one of the arrays has all the x,y values. I am also not sure how to deal with the missing values. I was thinking about making an array full of nan.

You want what is called an outer join. You have to use structured arrays:

In [2]: import numpy as np

In [3]: b_dtype = np.dtype([('i', int), ('j', int), ('b_word1', 'S8'),
('b_word2', 'S8')])

In [4]: bb = np.array([(1, 1, 'apple', 'pie'), (2, 5, 'boys', 'play')])

In [5]: bb
Out[5]:
array([['1', '1', 'apple', 'pie'],
['2', '5', 'boys', 'play']],
dtype='|S5')

In [6]: bb = np.array([(1, 1, 'apple', 'pie'), (2, 5, 'boys',
'play')], dtype=b_dtype)

In [7]: bb
Out[7]:
array([(1, 1, 'apple', 'pie'), (2, 5, 'boys', 'play')],
dtype=[('i', '<i4'), ('j', '<i4'), ('b_word1', '|S8'),
('b_word2', '|S8')])

In [8]: a_dtype = np.dtype([('i', int), ('j', int), ('a_word', 'S8')])

In [9]: aa = np.array([(1, 1, 'free'), (1, 2, 'upgrade'), (1, 3,
'down'), (2, 5, 'right')], dtype=a_dtype)

In [10]: aa
Out[10]:
array([(1, 1, 'free'), (1, 2, 'upgrade'), (1, 3, 'down'), (2, 5, 'right')],
dtype=[('i', '<i4'), ('j', '<i4'), ('a_word', '|S8')])

In [11]: from numpy.lib.recfunctions import join_by

In [12]: join_by(['i', 'j'], aa, bb, jointype='outer')
Out[12]:
masked_array(data = [(1, 1, 'free', 'apple', 'pie') (1, 2, 'upgrade', --, --)
(1, 3, 'down', --, --) (2, 5, 'right', 'boys', 'play')],
mask = [(False, False, False, False, False) (False,
False, False, True, True)
(False, False, False, True, True) (False, False, False, False, False)],
fill_value = (999999, 999999, 'N/A', 'N/A', 'N/A'),
dtype = [('i', '<i4'), ('j', '<i4'), ('a_word', '|S8'),
('b_word1', '|S8'), ('b_word2', '|S8')])

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
```

More information about the SciPy-User mailing list