-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayDataTypes.py
46 lines (35 loc) · 1.06 KB
/
ArrayDataTypes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr.dtype) # array with integer values
aa = np.array(['a', 'bloom', 'con'])
print(aa.dtype) # array with strings
###########################################################
# Creating arrays with defined data types
import numpy as np
ab = np.array([1, 2, 3, 4], dtype = 'S') # specifying that array should be of 'string' type
print(ab)
print(ab.dtype)
ac = np.array([6, 7, 8, 9], dtype='i4')
print(ac)
print(ac.dtype)
##########################################################
# Value Error
#ad = np.array(['a', '2', '3'], dtype='i')
# print(ad) # ValueError
##########################################################
# Converting Data Type on Existing Arrays 'astype()'
import numpy as np
ae = np.array([1.1, 2.2, 3.3])
newae = ae.astype('i') # also, ae.astype('int')
print(ae)
print(ae.dtype)
print(newae)
print(newae.dtype)
newar = ae.astype(bool)
print(newar)
print('\n')
af = np.array([1, 0, 3])
newaf = af.astype(bool)
print(af)
print(newaf)
print(newaf.dtype)