Python and NumPy numeric type inheritance relationship

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

Python and NumPy numeric type inheritance relationship
Python numeric type
Python and numpy tips
[python] week1-3: Number type and operation
Python #inheritance (inheritance)
Python basic course (4 numeric type / character string type)
My Numpy (Python)
Install and run Python3.5 + NumPy + SciPy on Windows 10
Relationship between Firestore and Go data type conversion
Python numpy ignores very small values ​​and displays
I compared "python dictionary type" and "excel function"
About python inheritance
Trouble with Python pseudo-private variables and class inheritance
#Python basics (#Numpy 2/2)
Python2 string type
Python # string type
Python #Numpy basics
[Python] Class type and usage of datetime module
[Python] Numpy memo
Relationship between operators and special methods, try calling numeric methods
Relationship data learning with numpy and NetworkX (spectral clustering)
[Tips] First-order difference calculation and inverse conversion [python / numpy]
[Python] Type Error: Summary of error causes and remedies for'None Type'
It's not easy to write Python, it's easy to write numpy and scipy
Python # How to check type and type for super beginners
Compare "relationship between log and infinity" in Gauche (0.9.4) and Python (3.5.1)
[python] Compress and decompress
[Python] pip and wheel
Python callable type specification
Batch design and python
Python packages and modules
Vue-Cli and Python integration
Ruby, Python and map
[Python] Class inheritance, override
[Python] Search (NumPy) ABC165C
python numpy array calculation
python input and output
Python and Ruby split
Check Python # type identity
Python3, venv and Ansible
Python asyncio and ContextVar
Python Basic --Pandas, Numpy-
[Introduction to cx_Oracle] (Part 6) DB and Python data type mapping
Let's create a PRML diagram with Python, Numpy and matplotlib.