NumPy - data type objects

https://numpy.org/doc/stable/reference/generated/numpy.dtype.html

https://numpy.org/doc/stable/reference/arrays.dtypes.html

INTRODUCTION

Default data type in Python: strings, integer, float, boolean, complex. NumPy has some extra data types.

A data type object describes how the bytes in the fixed-size block of memory corresponding to an array item should be interpreted.


# Using array-scalar types.

dt_int = np.dtype(int)   # Python-compatible integer
dt_int8 = np.dtype("int8")   # one byte integer

# "b" (byte), "i1", "int8";
# "h", "i2", "int16";
# "i", "i4", "int32"; "i8", "int64" ('int' 64-bit);
# unsigned int: "B", "u1", "uint8";
# "H", "u2", "uint16";
# "u4", "uint32";
# "u8", "uint64";

dt_float = np.dtype(float)   # Python-compatible floating-point number

# "f2", 'float16';
# "f", "f4", 'float32' ('float' in 32-bit);
# "d", "f8", 'float64' ('double', 'float' in 64-bit);
# 'float128' (np.longdouble);

dt_bool = np.dtype(bool)

dt_complex = np.dtype(complex)
# 'complex64', 'complex128', 'complex256'

dt_object = np.dtype(object)   # Python object

# Sized aliases to dtypes (useful for Numba).

assert np.dtype("int64") is np.dtype(np.int64)

np.int8, np.int16, np.int32, np.int64,
np.uint8, np.uint16, np.uint32, np.uint64,
np.float16, np.float32, np.float64,
np.complex64, np.complex128

np.sqrt(np.array([-1, 0, 1]))   # default dtype=float
# RuntimeWarning: invalid value encountered in sqrt
# array([nan,  0.,  1.])   # np.nan is used

np.sqrt(np.array([-1, 0, 1], dtype=complex))
# array([0.+1.j, 0.+0.j, 1.+0.j])

a = np.array([1, 2, 3], dtype=complex)

a.real   # or np.real(a)
# array([1., 2., 3.])

a.imag   # or np.imag(a)
# array([0., 0., 0.])

# Structured types, fields with names.

dt_gender = np.dtype([("gender","S1")])   # M/F string
dt_person = np.dtype([("name","S25"), ("age", "u1")])
dt_RGBA = np.dtype([('R','u1'), ('G','u1'), ('B','u1'), ('A','u1')])
dt_rgb = np.dtype({'names': ('r','g','b'), 'formats': ('u1','u1','u1')})

# Converting data types.

a = np.array([1.1, 0, 3.1])   # array([1.1, 0., 3.1]), dtype('float64')

b = a.astype(int)   # array([1, 0, 3]), copy, dtype('int64')

np.may_share_memory(a, b)   # False

c = a.astype(bool)   # array([ True, False, True]), dtype('bool')

EXERCISES

Conversion from string to int: ["1","2"], ["a","2"] (ValueError).