NumPy
numpy.org › doc › stable › reference › generated › numpy.ndarray.astype.html
numpy.ndarray.astype — NumPy v2.5 Manual
‘same_kind’ means only safe casts or casts within a kind, like float64 to float32, are allowed. ‘unsafe’ means any data conversions may be done. ‘same_value’ means any data conversions may be done, but the values must not change, including rounding of floats or overflow of ints · New in version 2.4: Support for 'same_value' was added. ... If True, then sub-classes will be passed-through (default), otherwise the returned array will be forced to be a base-class array. ... By default, astype always returns a newly allocated array.
NumPy
numpy.org › doc › stable › user › basics.types.html
Data types — NumPy v2.5 Manual
>>> z.astype(np.float64) array([0., 1., 2.]) Note that, above, we could have used the Python float object as a dtype instead of numpy.float64. NumPy knows that int refers to numpy.int_, bool means numpy.bool, that float is numpy.float64 and complex is numpy.complex128.
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: astype() to change dtype of an array | note.nkmk.me
February 4, 2024 - a_int = a.astype('int32') print(a_int) print(a_int.dtype) # [1 2 3] # int32 a_uint = a.astype('u8') print(a_uint) print(a_uint.dtype) # [1 2 3] # uint64 a_float = a.astype(float) print(a_float) print(a_float.dtype) # [1. 2. 3.] # float64 ... Converting from floating-point numbers (float) to integers (int) truncates the decimal part (rounding towards 0). a = np.array([-2, -1.5, -1, -0.5, 0.5, 1, 1.5, 2]) print(a.astype(int)) # [-2 -1 -1 0 0 1 1 2] ... Refer to the following articles for rounding, truncating, and ceiling in NumPy.
NumPy
numpy.org › doc › 2.1 › user › basics.types.html
Data types — NumPy v2.1 Manual
>>> z.astype(np.float64) array([0., 1., 2.]) Note that, above, we could have used the Python float object as a dtype instead of numpy.float64. NumPy knows that int refers to numpy.int_, bool means numpy.bool, that float is numpy.float64 and complex is numpy.complex128.
NumPy
numpy.org › devdocs › user › basics.types.html
Data types — NumPy v2.6.dev0 Manual
>>> z.astype(np.float64) array([0., 1., 2.]) Note that, above, we could have used the Python float object as a dtype instead of numpy.float64. NumPy knows that int refers to numpy.int_, bool means numpy.bool, that float is numpy.float64 and complex is numpy.complex128.
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.ndarray.astype.html
numpy.ndarray.astype — NumPy v2.1 Manual
‘same_kind’ means only safe casts or casts within a kind, like float64 to float32, are allowed. ‘unsafe’ means any data conversions may be done. ... If True, then sub-classes will be passed-through (default), otherwise the returned array will be forced to be a base-class array. ... By default, astype always returns a newly allocated array.
Programiz
programiz.com › python-programming › numpy › methods › astype
NumPy astype() | Programiz
August 30, 2023 - # casting with 'no' doesn't allow casting to any other data type array2 = array1.astype(array1.dtype, casting='no') # casting with 'equiv' allows casting to equivalent data types array3 = array1.astype('<i4', casting='equiv') #cCasting with 'safe' allows casting to safe data types preserving precision array4 = array1.astype(np.float64, casting='safe') # casting with 'same_kind' allows casting to data types of the same kind array5 = array1.astype(np.int32, casting='same_kind') # casting with 'unsafe' allows casting to any data type without checks array6 = array1.astype(str, casting='unsafe') print("Array with 'no' casting:", array2) print("Array with 'equiv' casting:", array3) print("Array with 'safe' casting:", array4) print("Array with 'same_kind' casting:", array5) print("Array with 'unsafe' casting:", array6)
NumPy
numpy.org › devdocs › reference › generated › numpy.ndarray.astype.html
numpy.ndarray.astype — NumPy v2.6.dev0 Manual
‘same_kind’ means only safe casts or casts within a kind, like float64 to float32, are allowed. ‘unsafe’ means any data conversions may be done. ‘same_value’ means any data conversions may be done, but the values must not change, including rounding of floats or overflow of ints · Added in version 2.4: Support for 'same_value' was added. ... If True, then sub-classes will be passed-through (default), otherwise the returned array will be forced to be a base-class array. ... By default, astype always returns a newly allocated array.
GeeksforGeeks
geeksforgeeks.org › numpy › change-numpy-array-data-type
Change the Data Type of the Given NumPy Array - GeeksforGeeks
The problems will help you understand astype() function and the changes in specific data types. Given a NumPy array whose underlying data is of 'int32' type. Change the data type of the given object to 'float64'.
Published: July 11, 2025
NumPy
numpy.org › doc › stable › reference › generated › numpy.astype.html
numpy.astype — NumPy v2.5 Manual
>>> import numpy as np >>> arr = np.array([1, 2, 3]); arr array([1, 2, 3]) >>> np.astype(arr, np.float64) array([1., 2., 3.]) Non-copy case: >>> arr = np.array([1, 2, 3]) >>> arr_noncpy = np.astype(arr, arr.dtype, copy=False) >>> np.shares_memory(arr, arr_noncpy) True ·
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.ndarray.astype.html
numpy.ndarray.astype — NumPy v2.0 Manual
‘same_kind’ means only safe casts or casts within a kind, like float64 to float32, are allowed. ‘unsafe’ means any data conversions may be done. ... If True, then sub-classes will be passed-through (default), otherwise the returned array will be forced to be a base-class array. ... By default, astype always returns a newly allocated array.
NumPy
numpy.org › doc › 2.2 › user › basics.types.html
Data types — NumPy v2.2 Manual
>>> z.astype(np.float64) array([0., 1., 2.]) Note that, above, we could have used the Python float object as a dtype instead of numpy.float64. NumPy knows that int refers to numpy.int_, bool means numpy.bool, that float is numpy.float64 and complex is numpy.complex128.
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.astype.html
numpy.astype — NumPy v2.0 Manual
>>> arr = np.array([1, 2, 3]); arr array([1, 2, 3]) >>> np.astype(arr, np.float64) array([1., 2., 3.])
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.ndarray.astype.html
numpy.ndarray.astype — NumPy v2.3 Manual
‘same_kind’ means only safe casts or casts within a kind, like float64 to float32, are allowed. ‘unsafe’ means any data conversions may be done. ... If True, then sub-classes will be passed-through (default), otherwise the returned array will be forced to be a base-class array. ... By default, astype always returns a newly allocated array.
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.ndarray.astype.html
numpy.ndarray.astype — NumPy v2.2 Manual
‘same_kind’ means only safe casts or casts within a kind, like float64 to float32, are allowed. ‘unsafe’ means any data conversions may be done. ... If True, then sub-classes will be passed-through (default), otherwise the returned array will be forced to be a base-class array. ... By default, astype always returns a newly allocated array.
NumPy
numpy.org › doc › 1.22 › user › basics.types.html
Data types — NumPy v1.22 Manual
Python’s floating-point numbers are usually 64-bit floating-point numbers, nearly equivalent to np.float64. In some unusual situations it may be useful to use floating-point numbers with more precision.
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.astype.html
numpy.astype — NumPy v2.1 Manual
>>> import numpy as np >>> arr = np.array([1, 2, 3]); arr array([1, 2, 3]) >>> np.astype(arr, np.float64) array([1., 2., 3.]) Non-copy case: >>> arr = np.array([1, 2, 3]) >>> arr_noncpy = np.astype(arr, arr.dtype, copy=False) >>> np.shares_memory(arr, arr_noncpy) True ·