On Thu, May 27, 2010 at 10:25 AM, Bruce Southey <bsout...@gmail.com> wrote:
> On 05/27/2010 10:40 AM, Vincent Davis wrote: > Can you give an example of what you are trying to do? > arr = np.array([(1,'a'),(2,'b')], dtype =[(num,int),(str, |s2)] No supposed I want to know if I can sum the values in 'num'. I could just try and then handle the exemption, but I would like to do something more like for col in arr.dtypes.names: if arr[col] "is a number": sum(arr[col]) I think i can use Roberts suggestion, I was not aware of np.number, I guess I need to look into the hierarchy more. The dtypes have a hierarchy. > > In [2]: np.issubdtype(float, np.number) > Out[2]: True > > In [3]: np.issubdtype(str, np.number) > Out[3]: False > > -- > Robert Kern Thanks Vincent > > > If some of your string arrays only have string representations of numbers > that you want to do the math on then you have to attempt to convert those > arrays into a numeric dtype (probably float) using for example asarray(). > > Bruce > > >>> import numpy as np > >>> a=np.array([1,2,3]) > >>> c=np.array(['1','2','3']) > >>> d=np.array(['a','b','1']) > >>> np.asarray(a, dtype=float) > array([ 1., 2., 3.]) > >>> np.asarray(c,dtype=float) > array([ 1., 2., 3.]) > >>> np.asarray(d,dtype=float) > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > File "/usr/lib64/python2.6/site-packages/numpy/core/numeric.py", line > 284, in asarray > return array(a, dtype, copy=False, order=order) > ValueError: invalid literal for float(): a > >>> try: > ... np.asarray(d,dtype=float) > ... except: > ... print 'fail' > ... > fail > > > > _______________________________________________ > NumPy-Discussion mailing list > NumPy-Discussion@scipy.org > http://mail.scipy.org/mailman/listinfo/numpy-discussion > > *Vincent Davis 720-301-3003 * vinc...@vincentdavis.net my blog <http://vincentdavis.net> | LinkedIn<http://www.linkedin.com/in/vincentdavis>
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion