I tried to find out if the NumPy numeric type inherits the corresponding Python numeric type.
import builtins
import numpy as np
np_numt = [
np.bool_,
np.int_,
np.intc,
np.intp,
np.int8,
np.int16,
np.int32,
np.int64,
np.uint8,
np.uint16,
np.uint32,
np.uint64,
np.float_,
np.float16,
np.float32,
np.float64,
np.complex_,
np.complex64,
np.complex128
]
py_numt = ["bool", "int", "float", "complex"]
for np_t in np_numt:
py_t = next(tstr for tstr in py_numt if tstr in str(np_t))
print(np_t, py_t, issubclass(np_t, getattr(builtins, py_t)))
When I run this code,
<class 'numpy.bool_'> bool False
<class 'numpy.int32'> int False
<class 'numpy.int32'> int False
<class 'numpy.int64'> int False
<class 'numpy.int8'> int False
<class 'numpy.int16'> int False
<class 'numpy.int32'> int False
<class 'numpy.int64'> int False
<class 'numpy.uint8'> int False
<class 'numpy.uint16'> int False
<class 'numpy.uint32'> int False
<class 'numpy.uint64'> int False
<class 'numpy.float64'> float True
<class 'numpy.float16'> float False
<class 'numpy.float32'> float False
<class 'numpy.float64'> float True
<class 'numpy.complex128'> complex True
<class 'numpy.complex64'> complex False
<class 'numpy.complex128'> complex True
And only numpy.float64
(and its alias numpy.float_
) and numpy.complex128
(and its alias numpy.complex_
) have the corresponding Python types float
and, respectively. It seems to be a subtype of complex
.
numpy.float64
is jsonable in the standard library json
, but note that the returned value is of Python type. For example, the following expression is of type float
in Python.
import json
type(json.loads(json.dumps(np.float64(64))))
Other NumPy numeric types will result in an error with the default json
.
Recommended Posts