[Numpy-discussion] recfunctions.stack_arrays

Pierre GM pgmdevlist@gmail....
Tue Jan 27 14:03:39 CST 2009

[Some background: we're talking about numpy.lib.recfunctions, a set of  
functions to manipulate structured arrays]

If the two files have the same structure, you can use that fact and  
specify the dtype of the output directly with the dtype parameter of  
mafromtxt. That way, you're sure that the two arrays will have the  
same dtype. If you don't know the structure beforehand, you could try  
to load one array and use its dtype as input of mafromtxt to load the  
second one.
Now, we could also try to modify stack_arrays so that it would take  
the largest dtype when several fields have the same name. I'm not  
completely satisfied by this approach, as it makes dtype conversions  
under the hood. Maybe we could provide the functionality as an option  
(w/ a forced_conversion boolean input parameter) ?
I'm a bit surprised by the error message you get. If I try:

 >>> a = ma.array([(1,2,3)], mask=[(0,1,0)], dtype=[('a',int),  
('b',bool), ('c',float)])
 >>> b = ma.array([(4, 5, 6)], dtype=[('a', int), ('b', float), ('c',  
 >>> test = np.stack_arrays((a, b))

I get a TypeError instead (the field 'b' hasn't the same type in a and  
b). Now, I get the 'two fields w/ the same name' when I use  
np.merge_arrays (with the flatten option). Could you send a small  
example ?

> P.S.  Thanks so much for your work on putting those utility  
> functions in
> recfunctions.py  It makes it so much easier to have these functions  
> available in
> the library itself rather than needing to reinvent the wheel over  
> and over.

Indeed. Note that most of the job had been done by John Hunter and the  
matplotlib developer in their matplotlib.mlab module, so you should  
thank them and not me. I just cleaned up some of the functions.

More information about the Numpy-discussion mailing list