The dtype constructors

There are many ways of creating data types. We can use the Python float, we can specify a single precision float, a double precision float, we could even pass dtype two character code, etc.

Examples:

Using float:

import numpy as np

np.dtype(float)

#out
dtype('float64')

Using single precision float:

import numpy as np

np.dtype(ā€˜fā€™)

#out
dtype('float32')

Using double precision float:

import numpy as np

np.dtype(ā€˜dā€™)

#out
dtype('float64')

Using two character code (the first character is the type, the second character specifies the number of bites in the type):

Note:
Numbers 2, 4, and 8 correspond to 16, 32, and 64 bits respectively.

Here is the list of all data type codes:

import numpy as np

np.sctypeDict.keys()

#out
dict_keys([0, 1, 2, 3, 4, 5, 6, 'I', 8, 9, 10, 11, 12, 13, 14, 15, 'void', 17, 18, 19, 20, 'object', 22, 23, 'str_', 'c8', 'UInt8', 'Complex128', 'cdouble', 'M8', 'ushort', 'uintc', 'G', 'clongdouble', 'm', 'f8', 'e', 'half', 7, 'complex_', 'int0', 'u4', 'S', 'double', 'O', 'Void0', 'UInt64', 'i4', 'single', 'complex128', 'datetime64', 'uint64', 'f4', 'int16', 'bool8', 'i2', 'Float64', 'Bool', 'void0', 'float32', 'clongfloat', 'c16', 'uint0', 'longlong', 'str0', 'bytes_', 'Complex64', 'd', 'c32', 'UInt32', 'P', 'longcomplex', 'long', 'F', 'a', 'f2', 'q', 'D', 'float128', 'u1', 'p', 'object_', 'u8', 'csingle', 'intp', 21, 'int64', 'f', 'singlecomplex', 'B', 'i8', 'cfloat', 'Complex32', 'complex', 'longdouble', 'int_', 'unicode', 'int8', 'uint', 'm8', 'bytes0', 'timedelta64', 'Float16', 'i', 'l', 'short', 'ubyte', 'uint16', 'b1', 'Int8', 'Object0', 'ulonglong', 'f16', 'u2', 'Float32', 'bool_', 'intc', 'bytes', 'int', 'float', 'h', 'float16', 'V', 'U', 'g', 'Datetime64', 'complex64', 'uint32', 'i1', 'unicode_', 'int32', 'Timedelta64', 'uint8', 'Float128', 'bool', 'b', 'Int64', 'Q', '?', 'uintp', 'L', 'H', 'Str0', 'object0', 16, 'string_', 'longfloat', 'complex256', 'Bytes0', 'M', 'Int16', 'float64', 'float_', 'UInt16', 'byte', 'Int32', 'str'])

Leave a Reply