On Sat, Dec 10, 2011 at 5:47 AM, ferreirafm <ferreirafm@lim12.fm.usp.br> wrote:


Hi Stéfan,
Thanks for your replay. Have a look in the arrays at:
http://ompldr.org/vYm83ZA
Regards,
Fred
--


I can recreate this error if tab is a structured ndarray - what is the dtype of tab?

If that is correct, I think you could fix this by simplifying things. Since tab is already an ndarray, you should not need to convert it back into a python list. By converting the ndarray back to a list you are making an extra level of "wrapping" as a python object, which is ultimately why you get that error about adding numpy.void.

Unfortunately you cannot take directly take a mean of a struct dtype; structs are generic so they could have fields with strings, or objects, etc, that would be invalid for a mean calculation. However the following code fragment should work pretty efficiently. It will make a 1-element array of the same dtype as tab, and then populate it with the mean value of all elements where the length is >= 15. Note that dtype.fields.keys() gives you a nice way to iterate over the fields in the struct dtype:

length_mask = tab['length'] >= 15
tab_means = np.zeros(1, dtype=tab.dtype)
for k in tab.dtype.fields.keys():
    tab_means[k] = np.mean( tab[k][mask] )

In general this would not work if tab has a field that is not a simple numeric type, such as a str, object, ... But it looks like your arrays are all numeric from your example above.

Hope that helps,
Aronne