numpy.issubdtype#
- numpy.issubdtype(arg1, arg2)[原始碼]#
如果第一個參數是類型階層中較低/相等的類型代碼,則返回 True。
這類似於內建的
issubclass
,但用於dtype
。- 參數:
- arg1, arg2dtype_like
dtype
或可強制轉換為 dtype 的物件
- 返回:
- outbool
另請參閱
- 純量
NumPy 類型階層的概觀。
範例
可以使用
issubdtype
來檢查陣列的類型>>> ints = np.array([1, 2, 3], dtype=np.int32) >>> np.issubdtype(ints.dtype, np.integer) True >>> np.issubdtype(ints.dtype, np.floating) False
>>> floats = np.array([1, 2, 3], dtype=np.float32) >>> np.issubdtype(floats.dtype, np.integer) False >>> np.issubdtype(floats.dtype, np.floating) True
不同大小的相似類型不是彼此的子類型
>>> np.issubdtype(np.float64, np.float32) False >>> np.issubdtype(np.float32, np.float64) False
但兩者都是
floating
的子類型>>> np.issubdtype(np.float64, np.floating) True >>> np.issubdtype(np.float32, np.floating) True
為了方便起見,也允許類似 dtype 的物件
>>> np.issubdtype('S1', np.bytes_) True >>> np.issubdtype('i4', np.signedinteger) True