From 990270306451e93443e970da131ce27f3c73e860 Mon Sep 17 00:00:00 2001 From: Rupa-Rd Date: Mon, 20 May 2024 23:03:59 +0530 Subject: [PATCH] Example type conversion is added --- contrib/numpy/datatypes.md | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/contrib/numpy/datatypes.md b/contrib/numpy/datatypes.md index d365623..12690b9 100644 --- a/contrib/numpy/datatypes.md +++ b/contrib/numpy/datatypes.md @@ -216,4 +216,35 @@ The `timedelta64()` used to find the difference between the `datetime64()`. The # 366 days # timedelta64[D] ``` -# Data Type Conversion \ No newline at end of file +# Data Type Conversion +`astype()` function is used to the NumPy object from one type to another type. + +It creates a copy of the array and allows to specify the data type of our choice. + +## Example 1 + +``` python + import numpy as np + + x = np.array([1.2, 3.4, 5.6]) + y = x.astype(int) + + print(y,y.dtype) + + # Output: + # [1 3 5] int64 +``` + +## Example 2 + +``` python + import numpy as np + + x = np.array([1, 3, 0]) + y = x.astype(bool) + + print(y,y.dtype) + + # Output: + # [True True False] bool +``` \ No newline at end of file