Skip to content

Commit b2cdbf4

Browse files
authored
Merge pull request animator#471 from Rupa-Rd/numpy-datatypes
Added Content: Numpy datatypes
2 parents 97e06aa + 024ab58 commit b2cdbf4

File tree

2 files changed

+268
-0
lines changed

2 files changed

+268
-0
lines changed

contrib/numpy/datatypes.md

Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
# Numpy Data Types
2+
In NumPy, data types play a crcial role in representing and manipulating numerical data.
3+
4+
Numpy supports the following data types:
5+
6+
- `i` - integer
7+
- `b` - boolean
8+
- `u` - unsigned integer
9+
- `f` - float
10+
- `c` - complex float
11+
- `m` - timedelta
12+
- `M` - datetime
13+
- `O` - object
14+
- `S` - string
15+
- `U` - unicode string
16+
17+
18+
_Referred from: W3schools_
19+
20+
## dtype() Function
21+
The `dtype()` function returns the type of the NumPy array object.
22+
23+
Example 1
24+
``` python
25+
import numpy as np
26+
27+
arr = np.array([1, 2, 3, 4])
28+
29+
print(arr.dtype)
30+
31+
# Output: int64
32+
```
33+
34+
Example 2
35+
``` python
36+
import numpy as np
37+
38+
arr = np.array(['apple', 'banana', 'cherry'])
39+
40+
print(arr.dtype)
41+
42+
# Output: <U6
43+
```
44+
## Example for integer type
45+
The NumPy integer array can be defined in two ways.
46+
47+
Way 1: Using function `int_()`
48+
``` python
49+
import numpy as np
50+
51+
arr = np.int_([2,4,6])
52+
# Size: int8, int16, int32, int64
53+
54+
print(arr.dtype())
55+
56+
# Output: int64
57+
```
58+
59+
Way 2: Using `dtype()`
60+
``` python
61+
import numpy as np
62+
63+
arr = np.array([2,4,6], dtype='i4')
64+
# Size: i1, i2, i4, i8
65+
66+
print(arr.dtype)
67+
68+
# Output: int32
69+
```
70+
71+
Note: `np.intc()` has the same function as `int32()`.
72+
## Example for float type
73+
74+
Way 1: Using function `float_()`
75+
``` python
76+
import numpy as np
77+
78+
arr = np.float_(1)
79+
# Size: float8, float16, float32, float64
80+
81+
print(arr)
82+
print(arr.dtype())
83+
84+
# Output:
85+
# 1.0
86+
# float64
87+
```
88+
89+
Way 2: Using `dtype()`
90+
``` python
91+
import numpy as np
92+
93+
arr = np.array([2,4,6], dtype='f4')
94+
# Size: f1, f2, f4, f8
95+
96+
print(arr)
97+
print(arr.dtype)
98+
99+
# Output:
100+
# [1. 2. 3. 4.]
101+
# float32
102+
```
103+
104+
Note: `np.single()` has the same function as `float32()`.
105+
106+
## Example for boolean type
107+
108+
``` python
109+
import numpy as np
110+
111+
x = np.bool_(1)
112+
113+
print(x)
114+
print(x.dtype)
115+
116+
# Output:
117+
# True
118+
# bool
119+
```
120+
## Example for unsigned integer type
121+
122+
``` python
123+
import numpy as np
124+
125+
x = np.uintc(1)
126+
127+
print(x)
128+
print(x.dtype)
129+
130+
# Output:
131+
# 1
132+
# uint32
133+
```
134+
135+
## Example for complex type
136+
Complex type is a combination of real number + imaginary number. The `complex_()` is used to define the complex type NumPy object.
137+
``` python
138+
import numpy as np
139+
140+
x = np.complex_(1)
141+
# Size: complex64, complex128
142+
143+
print(x)
144+
print(x.dtype)
145+
146+
# Output:
147+
# (1+0j)
148+
# complex128
149+
```
150+
151+
## Example for datetime type
152+
The `datetime64()` is used to define the date, month and year.
153+
154+
``` python
155+
import numpy as np
156+
157+
x = np.datetime64('2024-05')
158+
y = np.datetime64('2024-05-20')
159+
z = np.datetime64('2024')
160+
161+
print(x,x.dtype)
162+
print(y,y.dtype)
163+
print(z,z.dtype)
164+
165+
# Output:
166+
# 2024-05 datetime64[M]
167+
# 2024-20-05 datetime64[D]
168+
# 2024 datetime64[Y]
169+
```
170+
171+
## Example for string type
172+
``` python
173+
import numpy as np
174+
175+
arr = np.str_("roopa")
176+
177+
print(arr.dtype)
178+
179+
# Output: <U5
180+
```
181+
182+
## Example for object type
183+
``` python
184+
import numpy as np
185+
186+
arr = np.object_([1, 2, 3, 4])
187+
188+
print(arr)
189+
print(arr.dtype)
190+
191+
# Output:
192+
# [1, 2, 3, 4]
193+
# object
194+
```
195+
## Example for unicode string type
196+
``` python
197+
import numpy as np
198+
199+
arr = np.array(['apple', 'banana', 'cherry'])
200+
201+
print(arr.dtype)
202+
203+
# Output: <U6
204+
```
205+
## Example for timedelta type
206+
The `timedelta64()` used to find the difference between the `datetime64()`. The arguments for timedelta64 are a number, to represent the number of units, and a date/time unit, such as (D)ay, (M)onth, (Y)ear, (h)ours, (m)inutes, or (s)econds. The timedelta64 data type also accepts the string “NAT” in place of the number for a “Not A Time” value.
207+
208+
``` python
209+
import numpy as np
210+
211+
x = np.datetime64('2024-05-20')
212+
y = np.datetime64('2023-05-20')
213+
res = x - y
214+
215+
print(res)
216+
print(res.dtype)
217+
218+
# Output:
219+
# 366 days
220+
# timedelta64[D]
221+
```
222+
## Additional Data Type (`longdouble`)
223+
`longdouble` is a data type that provides higher precision than the standard double-precision floating-point (`float64`) type.
224+
225+
``` python
226+
import numpy as np
227+
228+
arr = np.longdouble([1.222222, 4.44, 45.55])
229+
230+
print(arr, arr.dtype)
231+
232+
# Output:
233+
# [1.222222 4.44 45.55] float128
234+
```
235+
236+
# Data Type Conversion
237+
`astype()` function is used to the NumPy object from one type to another type.
238+
239+
It creates a copy of the array and allows to specify the data type of our choice.
240+
241+
## Example 1
242+
243+
``` python
244+
import numpy as np
245+
246+
x = np.array([1.2, 3.4, 5.6])
247+
y = x.astype(int)
248+
249+
print(y,y.dtype)
250+
251+
# Output:
252+
# [1 3 5] int64
253+
```
254+
255+
## Example 2
256+
257+
``` python
258+
import numpy as np
259+
260+
x = np.array([1, 3, 0])
261+
y = x.astype(bool)
262+
263+
print(y,y.dtype)
264+
265+
# Output:
266+
# [True True False] bool
267+
```

contrib/numpy/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
- [Installing NumPy](installing-numpy.md)
44
- [Introduction](introduction.md)
5+
- [NumPy Data Types](datatypes.md)

0 commit comments

Comments
 (0)