On 05/27/2010 10:40 AM, Vincent Davis wrote:
Can you give an example of what you are trying to do?
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
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 | |