How to distinguish between number and string dypes
How do I determine if an array's (or column in a structured array) dtype is a number or a string. I see how to determine the actual dtype but all I want to know is if it is a string or a number. *Vincent Davis 7203013003 * vincent@vincentdavis.net my blog <http://vincentdavis.net>  LinkedIn<http://www.linkedin.com/in/vincentdavis>
On May 26, 2010, at 11:52 PM, Vincent Davis wrote:
How do I determine if an array's (or column in a structured array) dtype is a number or a string. I see how to determine the actual dtype but all I want to know is if it is a string or a number.
Check `numpy.lib._iotools._is_string_like`
A Thursday 27 May 2010 05:52:22 Vincent Davis escrigué:
How do I determine if an array's (or column in a structured array) dtype is a number or a string. I see how to determine the actual dtype but all I want to know is if it is a string or a number.
I suppose that the `.kind` attribute of dtype would help you: In [2]: s = np.dtype("S3") In [4]: s.kind Out[4]: 'S' In [5]: i = np.dtype("i4") In [6]: i.kind Out[6]: 'i' In [7]: f = np.dtype("f8") In [8]: f.kind Out[8]: 'f'  Francesc Alted
On Thu, May 27, 2010 at 1:27 AM, Francesc Alted <faltet@pytables.org> wrote:
A Thursday 27 May 2010 05:52:22 Vincent Davis escrigué:
How do I determine if an array's (or column in a structured array) dtype is a number or a string. I see how to determine the actual dtype but all I want to know is if it is a string or a number.
I suppose that the `.kind` attribute of dtype would help you:
In [2]: s = np.dtype("S3")
In [4]: s.kind Out[4]: 'S'
In [5]: i = np.dtype("i4")
In [6]: i.kind Out[6]: 'i'
In [7]: f = np.dtype("f8")
In [8]: f.kind Out[8]: 'f'
I know about this but the problem is that while the fist example is usable, the others are not great because to know that it is a number I would need to do something like(see below) but I might miss a number dtype, def is_number(obj): if obj.dtype.kind in ('i', 'f',..): return True Pierre GM "Check `numpy.lib._iotools._is_string_like`" This is ok, but I am having problem making it work, I keep getting an error that I am giving it 2 items and it only takes 1. Obviously I think I am giving it 1. This of course tells me if it is string like but not that "is" a number. Thanks Vincent
 Francesc Alted _______________________________________________ NumPyDiscussion mailing list NumPyDiscussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpydiscussion
*Vincent Davis 7203013003 * vincent@vincentdavis.net my blog <http://vincentdavis.net>  LinkedIn<http://www.linkedin.com/in/vincentdavis>
On Thu, May 27, 2010 at 6:02 AM, Vincent Davis <vincent@vincentdavis.net> wrote:
On Thu, May 27, 2010 at 1:27 AM, Francesc Alted <faltet@pytables.org> wrote:
A Thursday 27 May 2010 05:52:22 Vincent Davis escrigué:
How do I determine if an array's (or column in a structured array) dtype is a number or a string. I see how to determine the actual dtype but all I want to know is if it is a string or a number.
I suppose that the `.kind` attribute of dtype would help you:
In [2]: s = np.dtype("S3")
In [4]: s.kind Out[4]: 'S'
In [5]: i = np.dtype("i4")
In [6]: i.kind Out[6]: 'i'
In [7]: f = np.dtype("f8")
In [8]: f.kind Out[8]: 'f'
I know about this but the problem is that while the fist example is usable, the others are not great because to know that it is a number I would need to do something like(see below) but I might miss a number dtype, def is_number(obj): if obj.dtype.kind in ('i', 'f',..): return True
Pierre GM "Check `numpy.lib._iotools._is_string_like`"
This is ok, but I am having problem making it work, I keep getting an error that I am giving it 2 items and it only takes 1. Obviously I think I am giving it 1. This of course tells me if it is string like but not that "is" a number. Thanks Vincent
To see if it is a number could you use something like: np.issubdtype(a.dtype, float) or np.issubdtype(a.dtype, int) or np.issubdtype(a.dtype, complex) And for string: np.issubdtype(a.dtype, str)
On Thu, May 27, 2010 at 8:39 AM, Keith Goodman <kwgoodman@gmail.com> wrote:
To see if it is a number could you use something like:
np.issubdtype(a.dtype, float) or np.issubdtype(a.dtype, int) or np.issubdtype(a.dtype, complex)
And for string:
np.issubdtype(a.dtype, str)
These are valid but what I don't like is that I need to know the list of possible number types. Basically I don't like a test that fails because I didn't know about a dtype. For string It is ok, the universe of is either string or not string. Maybe this is as good as it gets. I guess my use case is that I want to be sure I can perform math on the values. So maybe I should just do someting like "numpy.lib._iotools._is_string_like" but "_is_number_like", Maybe there is such and I missed it. If not there should be. Vincent _______________________________________________
NumPyDiscussion mailing list NumPyDiscussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpydiscussion
*Vincent Davis 7203013003 * vincent@vincentdavis.net my blog <http://vincentdavis.net>  LinkedIn<http://www.linkedin.com/in/vincentdavis>
On Thu, May 27, 2010 at 11:40, Vincent Davis <vincent@vincentdavis.net> wrote:
On Thu, May 27, 2010 at 8:39 AM, Keith Goodman <kwgoodman@gmail.com> wrote:
To see if it is a number could you use something like: np.issubdtype(a.dtype, float) or np.issubdtype(a.dtype, int) or np.issubdtype(a.dtype, complex)
And for string:
np.issubdtype(a.dtype, str)
These are valid but what I don't like is that I need to know the list of possible number types. Basically I don't like a test that fails because I didn't know about a dtype. For string It is ok, the universe of is either string or not string. Maybe this is as good as it gets.
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 "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth."  Umberto Eco
On 05/27/2010 10:40 AM, Vincent Davis wrote:
On Thu, May 27, 2010 at 8:39 AM, Keith Goodman <kwgoodman@gmail.com <mailto:kwgoodman@gmail.com>> wrote:
To see if it is a number could you use something like:
np.issubdtype(a.dtype, float) or np.issubdtype(a.dtype, int) or np.issubdtype(a.dtype, complex)
And for string:
np.issubdtype(a.dtype, str)
These are valid but what I don't like is that I need to know the list of possible number types. Basically I don't like a test that fails because I didn't know about a dtype. For string It is ok, the universe of is either string or not string. Maybe this is as good as it gets.
I guess my use case is that I want to be sure I can perform math on the values. So maybe I should just do someting like "numpy.lib._iotools._is_string_like" but "_is_number_like", Maybe there is such and I missed it. If not there should be.
Vincent
Can you give an example of what you are trying to do? 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/sitepackages/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
On Thu, May 27, 2010 at 10:25 AM, Bruce Southey <bsouthey@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/sitepackages/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
_______________________________________________ NumPyDiscussion mailing list NumPyDiscussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpydiscussion
*Vincent Davis 7203013003 * vincent@vincentdavis.net my blog <http://vincentdavis.net>  LinkedIn<http://www.linkedin.com/in/vincentdavis>
participants (6)

Bruce Southey

Francesc Alted

Keith Goodman

Pierre GM

Robert Kern

Vincent Davis