Numpy with Data Type

import numpy as np
person = np.dtype([('name,', 'S5'), ('code', 'i2')])
p_array = np.array([('one', 21)], dtype=person)
p_array
array([(b'one', 21)], dtype=[('name,', 'S5'), ('code', '<i2')])
print(p_array)
[(b'one', 21)]
p_array = np.array([('David', 200), ('Sam', 202), ('KevinKalvin', 203)], dtype=person)
print(p_array)
[(b'David', 200) (b'Sam', 202) (b'Kevin', 203)]
# note: even though we passed KevinKalvin, it prints only 5 letters as the dataype allows only 5 characters

Other data types

‘b’ − boolean

‘i’ − (signed) integer

‘u’ − unsigned integer

‘f’ − floating-point

‘c’ − complex-floating point

’m’ − timedelta

’M’ − datetime

‘O’ − (Python) objects

’S’, ‘a’ − (byte-)string

‘U’ − Unicode

‘V’ − raw data (void)

person1 = np.dtype([('name,', 'S5'), ('code', 'i1')])
y_array = np.array([('David', 200), ('Sam', 220), ('KevinKalvin', 299)], dtype=person1)
print(y_array)
[(b'David', -56) (b'Sam', -36) (b'Kevin',  43)]
# Check the values go minus (-56). It happens because of i1 restriction