[Numpy-discussion] combining recarrays

Keith Goodman kwgoodman@gmail....
Tue Dec 30 12:15:59 CST 2008


On Tue, Dec 30, 2008 at 10:10 AM, ctw <lists.20.chth@xoxy.net> wrote:
> Hi!
>
> I'm a bit stumped by the following: suppose I have several recarrays
> with identical dtypes (identical field names, etc.) and would like to
> combine them into one rec array, what would be the best way to do
> that? I tried using np.rec.fromrecords, but that doesn't produce the
> desired result. As a minimal example consider the following code:
>
> desc = np.dtype({'names':['a','b'],'formats':[np.float,np.int]})
> rec1 = np.zeros(3,desc)
> rec2 = np.zeros(3,desc)
>
> Now I have two recarrays of shape (3,) that both look like this:
> array([(0.0, 0), (0.0, 0), (0.0, 0)],
>      dtype=[('a', '<f8'), ('b', '<i4')])
>
> I would like to turn them into one new recarray of shape (6,) that
> looks like this:
> array([(0.0, 0), (0.0, 0), (0.0, 0), (0.0, 0), (0.0, 0), (0.0, 0)],
>      dtype=[('a', '<f8'), ('b', '<i4')])
>
> Any ideas?

I'm not familiar with rec arrays, but this should work for any array:

>> np.r_[rec1, rec2]

array([(0.0, 0), (0.0, 0), (0.0, 0), (0.0, 0), (0.0, 0), (0.0, 0)],
      dtype=[('a', '<f8'), ('b', '<i4')])

>> np.concatenate((rec1, rec2), 0)

array([(0.0, 0), (0.0, 0), (0.0, 0), (0.0, 0), (0.0, 0), (0.0, 0)],
      dtype=[('a', '<f8'), ('b', '<i4')])


More information about the Numpy-discussion mailing list