Reputation: 6869
I am looking for a way to check if a numpy array is np.float64
or np.float32
. This works fine for np.float64
:
a = np.random.rand(10)
if not issubclass(a.dtype.type, np.float):
raise "Wrong type" # No exception is raised for np.float64
But fails for np.float32
:
a = np.random.rand(10).astype(np.float32)
if not issubclass(a.dtype.type, np.float):
raise "Wrong type" # An exception is raised!
Upvotes: 5
Views: 1640
Reputation: 16179
One way you can check if a data type is a float is with issubdtype
:
In [1]: a = np.random.rand(10).astype(np.float64)
In [2]: b = np.random.rand(10).astype(np.float32)
In [3]: np.issubdtype(a.dtype,np.floating)
Out[3]: True
In [4]: np.issubdtype(b.dtype,np.floating)
Out[4]: True
Upvotes: 4