Skip to content

Commit 9902703

Browse files
committed
Example type conversion is added
1 parent cf4a924 commit 9902703

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

contrib/numpy/datatypes.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,35 @@ The `timedelta64()` used to find the difference between the `datetime64()`. The
216216
# 366 days
217217
# timedelta64[D]
218218
```
219-
# Data Type Conversion
219+
# Data Type Conversion
220+
`astype()` function is used to the NumPy object from one type to another type.
221+
222+
It creates a copy of the array and allows to specify the data type of our choice.
223+
224+
## Example 1
225+
226+
``` python
227+
import numpy as np
228+
229+
x = np.array([1.2, 3.4, 5.6])
230+
y = x.astype(int)
231+
232+
print(y,y.dtype)
233+
234+
# Output:
235+
# [1 3 5] int64
236+
```
237+
238+
## Example 2
239+
240+
``` python
241+
import numpy as np
242+
243+
x = np.array([1, 3, 0])
244+
y = x.astype(bool)
245+
246+
print(y,y.dtype)
247+
248+
# Output:
249+
# [True True False] bool
250+
```

0 commit comments

Comments
 (0)