How to convert the type of a NumPy array in-place in Python

A NumPy array has a specified datatype for all its values. The datatype of a NumPy array can be changed without allocating a new array only when the two datatypes use the same number of bytes. For example, converting the bytes of the array [[1, 2], [3, 4]] from int32 to float32 results in [[1., 2.], [3., 4.]].

Solution for How to convert the type of a NumPy array in-place in Python : You can use numpy.ndarray.astype() to convert the type of a NumPy array in-place Use syntax numpy.ndarray.astype(dtype, copy=False) to convert numpy.ndarray to dtype without allocating new memory by reinterpreting the bytes of numpy.ndarray.


how-to-convert-the-type-of-a-numpy-array-in-place-in-python