[SciPy-User] determining types in dtype
Skipper Seabold
jsseabold@gmail....
Sun Mar 21 12:14:45 CDT 2010
Is there an easy way to determine if the dtype of a (non-nested)
structured-array is homogenous/similar? Basically, I want to know if
all elements are int and/or float so it can be safely cast to an
ndarray or if it contains strings (or, more generally, other objects)
so it needs some more processing.
I thought issctype might provide what I'm looking for but
import numpy as np
X = np.array([('1', 1.0), ('1', 1.0), ('1', 1.0)],
dtype=[('foo', 'a1'), ('bar', '<f8')])
np.issctype(X.dtype)
# True
Similar result for nested structs of string type
Y = np.array([(('1','2'), 1.0), (('1','2'), 1.0), (('1','2'), 1.0)],
dtype=[('foo', 'a1', (1,2)), ('bar', '<f8')])
np.issctype(Y.dtype)
# True
In particular, I find this odd
np.issctype(str)
# True
I would expect this to be like
np.issctype(object)
# False
Unless there is something I don't understand about types, which is
probably the case.
The only other way I can think of is to use X.dtype.descr and actually
parse the list to determine the types. Any thoughts?
Skipper
More information about the SciPy-User
mailing list