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

Vincent Davis vincent at vincentdavis.net
Sat Mar 6 18:32:22 EST 2010


Thank you, that is exactly what I needed, and I was already using structured
arrays.

  *Vincent Davis
720-301-3003 *
vincent at vincentdavis.net
 my blog <http://vincentdavis.net> |
LinkedIn<http://www.linkedin.com/in/vincentdavis>


On Sat, Mar 6, 2010 at 3:30 PM, Robert Kern <robert.kern at gmail.com> wrote:

> On Sat, Mar 6, 2010 at 15:46, Vincent Davis <vincent at 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
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20100306/8bae2a69/attachment.html>


More information about the SciPy-User mailing list