There is actually a way to do this built into Numpy :
x_np = np.random.rand(10, 10) typecodes = np.ctypeslib._get_typecodes() typecodes[x_np.__array_interface__['typestr']]
Output:
ctypes.c_double
np.ctypeslib._get_typecodes means that the np.ctypeslib._get_typecodes function np.ctypeslib._get_typecodes marked as private (i.e. np.ctypeslib._get_typecodes name starts with _ ). However, its implementation does not seem to have changed over time, so you can probably use it reliably enough.
Alternatively, the implementation of _get_typecodes rather short, so you can simply copy the entire function into your own code:
import ctypes import numpy as np def get_typecodes(): ct = ctypes simple_types = [ ct.c_byte, ct.c_short, ct.c_int, ct.c_long, ct.c_longlong, ct.c_ubyte, ct.c_ushort, ct.c_uint, ct.c_ulong, ct.c_ulonglong, ct.c_float, ct.c_double, ] return {np.dtype(ctype).str: ctype for ctype in simple_types}
source share