simpliest way to check: array x is float, not integer
hi all, please inform me what is the simplest way to check, does the vector x that came to my func is float or integer. I.e. someone can pass to my func for example x0 = numpy.array([1, 0, 0]) and it can yield wrong unexpected results vs numpy.array([1.0, 0, 0]) . Thx, D.
dmitrey wrote:
hi all, please inform me what is the simplest way to check, does the vector x that came to my func is float or integer. I.e. someone can pass to my func for example x0 = numpy.array([1, 0, 0]) and it can yield wrong unexpected results vs numpy.array([1.0, 0, 0]) .
Usually, it's not so much that you need to know whether a parameter is a float or an integer, but rather that your function is designed to work with one or the other. In that case, you can force it with as array: def test(InputArray): InputArray = numpy.asarray(InputArray, numpy.float) asarray is a noop if the input is already of the type you've specified It's handy, as it will take *anything* that can be turned into an array of the specified type. This won't work the function needs to change the input in place, however. Chris  Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 5266959 voice 7600 Sand Point Way NE (206) 5266329 fax Seattle, WA 98115 (206) 5266317 main reception Chris.Barker@noaa.gov
When using numpy array the type of the array is given by the "dtype" variable of the array. So if your array is int then array.dtype will be 'int32'. Numpy uses more complex data types then just int and floats so you might want to check all the available data types. Ex: In [168]: a = array([1,2,3]) In [169]: a.dtype Out[169]: dtype('int32') OR (Array) In [170]: b = array([1.,2.,3.]) In [171]: b.dtype Out[171]: dtype('float64') OR (Strings) In [172]: c = array(['sdf', 'fff', 'tye']) In [173]: c.dtype Out[173]: dtype('S3') # This could also be 'Sxxx' where xxx is the length of the largest string in the array Alternatively, as a hackjob type check you could also do an "isinstance" check on the first element of the array since, unlike lists, arrays have uniform elements all the way through. Hope that helps,  Simon Berube P.S.: Someone please correct me if there's a better way. On Apr 29, 6:51 am, dmitrey <open...@ukr.net> wrote:
hi all, please inform me what is the simplest way to check, does the vector x that came to my func is float or integer. I.e. someone can pass to my func for example x0 = numpy.array([1, 0, 0]) and it can yield wrong unexpected results vs numpy.array([1.0, 0, 0]) . Thx, D.
_______________________________________________ Numpydiscussion mailing list Numpydiscuss...@scipy.orghttp://projects.scipy.org/mailman/listinfo/numpydiscussion
On Tue, May 01, 2007 at 12:05:20PM 0000, Simon Berube wrote:
Alternatively, as a hackjob type check you could also do an "isinstance" check on the first element of the array since, unlike lists, arrays have uniform elements all the way through.
Or use N.issubdtype(x.dtype,int) and N.issubdtype(x.dtype,float) Cheers StÃ©fan
participants (4)

Christopher Barker

dmitrey

Simon Berube

Stefan van der Walt