Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stacking numpy recarrays without losing their recarrayness

Suppose I make two recarrays with the same dtype and stack them:

>>> import numpy as np
>>> dt = [('foo', int), ('bar', float)]
>>> a = np.empty(2, dtype=dt).view(np.recarray)
>>> b = np.empty(3, dtype=dt).view(np.recarray)
>>> c = np.hstack((a,b))

Although a and b are recarrays, c is not:

>>> c.foo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'numpy.ndarray' object has no attribute 'foo'
>>> d = c.view(np.recarray)
>>> d.foo
array([                  0,     111050731618561,                   0,
                   7718048, 8246760947200437872])

I can obviously turn it into a recarray again, as shown with d above, but that is inconvenient. Is there a reason why stacking two recarrays does not produce another recarray?

like image 832
Vebjorn Ljosa Avatar asked Nov 24 '09 17:11

Vebjorn Ljosa


1 Answers

Alternatively, there are some helper utilities in numpy.lib.recfunctions which I stumbled across here. This module has functions for both merging and stacking recarrays:

from numpy.lib.recfunctions import stack_arrays
c = stack_arrays((a, b), asrecarray=True, usemask=False)
c.foo
>>> array([     140239282560000,           4376479720, -4611686018427387904,
                     4358733828,           4365061216])

If one wants to add extra columns to a recarray, this can be done using merge_arrays:

import numpy as np
from numpy.lib.recfunctions import merge_arrays
dt1 = [('foo', int), ('bar', float)]
dt2 = [('foobar', int), ('barfoo', float)]
aa = np.empty(6, dtype=dt1).view(np.recarray)
bb = np.empty(6, dtype=dt2).view(np.recarray)

cc = merge_arrays((aa, bb), asrecarray=True, flatten=True)
type(cc)
>>> numpy.core.records.recarray

(Although not an answer to the question, I'm posting the latter example as a reference)

like image 77
Tim Avatar answered Sep 22 '22 14:09

Tim