Laying multi-level reparations without losing their repeatability

Suppose I do two repetitions with the same type and add 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 recursions, c 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 include it again in the recheck, as shown above with d , but this is inconvenient. Is there a reason why stacking two recursive data does not lead to another repetition?

+6
python numpy recarray
source share
3 answers

I dont know. This is most likely an error / function that has never been implemented. numpy.hstack is basically a wrapper around a function in numpy.core.fromnumeric . Numeric is one of two numpy predecessors. Most functions in numpy have an agreement for output of the same type as input, calling the __array_wrap__ input method on the output, and the resulting output should have the same data, but "wrapped" in a new class. Perhaps the concept of β€œwrapping” was not in numerical form and was never added to this function.

You can use this technique to make smarter styling function.

 def hstack2(arrays) : return arrays[0].__array_wrap__(numpy.hstack(arrays)) 

This works for both recursive and regular arrays.

 >>> f = hstack2((a,b)) >>> type(f) <class 'numpy.core.records.recarray'> >>> f.foo array([ 140633760262784, 111050731618561, 140633760262800, 7536928, 8391166428122670177]) >>> x = numpy.random.rand(3) >>> y = numpy.random.rand(2) >>> z = hstack2((x,y)) >>> type(z) <type 'numpy.ndarray'> 

I'm not sure what you are planning, but you can ask the numpy mailing list there is a better way than using the documented but bidirectional method and that their arguments are that they do not wrap.

+6
source share

As an alternative, there are helper utilities numpy.lib.recfunctions that I came across. This module has functions for 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 you want to add additional columns to recarray , this can be done with 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 this is not an answer to the question, I am posting the last example as a reference)

+5
source share

By the way, you can also use:

 c = np.concatenate((a,b)) 

or

 c = np.r_[a, b] 

(Source: This is a mailing list message )

-one
source share

All Articles