Let's see if I can address some of the confusion I'm seeing in the comments.

Make an array:

In [609]: x=np.arange(5)
In [610]: x
Out[610]: array([0, 1, 2, 3, 4])
In [611]: x.dtype
Out[611]: dtype('int32')

The default for arange is to make an int32.

astype is an array method; it can used on any array:

In [612]: x.astype(np.float32)
Out[612]: array([ 0.,  1.,  2.,  3.,  4.], dtype=float32)

arange also takes a dtype parameter

In [614]: np.arange(5, dtype=np.float32)
Out[614]: array([ 0.,  1.,  2.,  3.,  4.], dtype=float32)

whether it created the int array first and converted it, or made the float32 directly isn't any concern to me. This is a basic operation, done in compiled code.

I can also give it a float stop value, in which case it will give me a float array - the default float type.

In [615]: np.arange(5.0)
Out[615]: array([ 0.,  1.,  2.,  3.,  4.])
In [616]: _.dtype
Out[616]: dtype('float64')

zeros is similar; the default dtype is float64, but with a parameter I can change that. Since its primary task with to allocate memory, and it doesn't have to do any calculation, I'm sure it creates the desired dtype right away, without further conversion. But again, this is compiled code, and I shouldn't have to worry about what it is doing under the covers.

In [618]: np.zeros(5)
Out[618]: array([ 0.,  0.,  0.,  0.,  0.])
In [619]: _.dtype
Out[619]: dtype('float64')
In [620]: np.zeros(5,dtype=np.float32)
Out[620]: array([ 0.,  0.,  0.,  0.,  0.], dtype=float32)

randn involves a lot of calculation, and evidently it is compiled to work with the default float type. It does not take a dtype. But since the result is an array, it can be cast with astype.

In [623]: np.random.randn(3)
Out[623]: array([-0.64520949,  0.21554705,  2.16722514])
In [624]: _.dtype
Out[624]: dtype('float64')
In [625]: __.astype(np.float32)
Out[625]: array([-0.64520949,  0.21554704,  2.16722512], dtype=float32)

Let me stress that astype is a method of an array. It takes the values of the array and produces a new array with the desire dtype. It does not act retroactively (or in-place) on the array itself, or on the function that created that array.

The effect of astype is often (always?) the same as a dtype parameter, but the sequence of actions is different.

In https://stackoverflow.com/a/39625960/901925 I describe a sparse matrix creator that takes a dtype parameter, and implements it with an astype method call at the end.

When you do calculations such as dot or *, it tries to match the output dtype with inputs. In the case of mixed types it goes with the higher precision alternative.

In [642]: np.arange(5,dtype=np.float32)*np.arange(5,dtype=np.float64)
Out[642]: array([  0.,   1.,   4.,   9.,  16.])
In [643]: _.dtype
Out[643]: dtype('float64')
In [644]: np.arange(5,dtype=np.float32)*np.arange(5,dtype=np.float32)
Out[644]: array([  0.,   1.,   4.,   9.,  16.], dtype=float32)

There are casting rules. One way to look those up is with can_cast function:

In [649]: np.can_cast(np.float64,np.float32)
Out[649]: False
In [650]: np.can_cast(np.float32,np.float64)
Out[650]: True

It is possible in some calculations that it will cast the 32 to 64, do the calculation, and then cast back to 32. The purpose would be to avoid rounding errors. But I don't know how you find that out from the documentation or tests.

Answer from hpaulj on Stack Overflow
🌐
Programiz
programiz.com › python-programming › numpy › methods › astype
NumPy astype()
The astype() method converts an array to a specified data type. import numpy as np # original array of integers integerArray = np.array([1, 2, 3, 4, 5])
🌐
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 › 1.13 › reference › generated › numpy.ndarray.astype.html
numpy.ndarray.astype — NumPy v1.13 Manual
June 10, 2017 - Starting in NumPy 1.9, astype method now returns an error if the string dtype to cast to is not long enough in ‘safe’ casting mode to hold the max value of integer/float array that is being casted.
Top answer
1 of 3
13

Let's see if I can address some of the confusion I'm seeing in the comments.

Make an array:

In [609]: x=np.arange(5)
In [610]: x
Out[610]: array([0, 1, 2, 3, 4])
In [611]: x.dtype
Out[611]: dtype('int32')

The default for arange is to make an int32.

astype is an array method; it can used on any array:

In [612]: x.astype(np.float32)
Out[612]: array([ 0.,  1.,  2.,  3.,  4.], dtype=float32)

arange also takes a dtype parameter

In [614]: np.arange(5, dtype=np.float32)
Out[614]: array([ 0.,  1.,  2.,  3.,  4.], dtype=float32)

whether it created the int array first and converted it, or made the float32 directly isn't any concern to me. This is a basic operation, done in compiled code.

I can also give it a float stop value, in which case it will give me a float array - the default float type.

In [615]: np.arange(5.0)
Out[615]: array([ 0.,  1.,  2.,  3.,  4.])
In [616]: _.dtype
Out[616]: dtype('float64')

zeros is similar; the default dtype is float64, but with a parameter I can change that. Since its primary task with to allocate memory, and it doesn't have to do any calculation, I'm sure it creates the desired dtype right away, without further conversion. But again, this is compiled code, and I shouldn't have to worry about what it is doing under the covers.

In [618]: np.zeros(5)
Out[618]: array([ 0.,  0.,  0.,  0.,  0.])
In [619]: _.dtype
Out[619]: dtype('float64')
In [620]: np.zeros(5,dtype=np.float32)
Out[620]: array([ 0.,  0.,  0.,  0.,  0.], dtype=float32)

randn involves a lot of calculation, and evidently it is compiled to work with the default float type. It does not take a dtype. But since the result is an array, it can be cast with astype.

In [623]: np.random.randn(3)
Out[623]: array([-0.64520949,  0.21554705,  2.16722514])
In [624]: _.dtype
Out[624]: dtype('float64')
In [625]: __.astype(np.float32)
Out[625]: array([-0.64520949,  0.21554704,  2.16722512], dtype=float32)

Let me stress that astype is a method of an array. It takes the values of the array and produces a new array with the desire dtype. It does not act retroactively (or in-place) on the array itself, or on the function that created that array.

The effect of astype is often (always?) the same as a dtype parameter, but the sequence of actions is different.

In https://stackoverflow.com/a/39625960/901925 I describe a sparse matrix creator that takes a dtype parameter, and implements it with an astype method call at the end.

When you do calculations such as dot or *, it tries to match the output dtype with inputs. In the case of mixed types it goes with the higher precision alternative.

In [642]: np.arange(5,dtype=np.float32)*np.arange(5,dtype=np.float64)
Out[642]: array([  0.,   1.,   4.,   9.,  16.])
In [643]: _.dtype
Out[643]: dtype('float64')
In [644]: np.arange(5,dtype=np.float32)*np.arange(5,dtype=np.float32)
Out[644]: array([  0.,   1.,   4.,   9.,  16.], dtype=float32)

There are casting rules. One way to look those up is with can_cast function:

In [649]: np.can_cast(np.float64,np.float32)
Out[649]: False
In [650]: np.can_cast(np.float32,np.float64)
Out[650]: True

It is possible in some calculations that it will cast the 32 to 64, do the calculation, and then cast back to 32. The purpose would be to avoid rounding errors. But I don't know how you find that out from the documentation or tests.

2 of 3
3
arr1 = np.array([25, 56, 12, 85, 34, 75])    
arr2 = np.array([42, 3, 86, 32, 856, 46])

arr1.astype(np.complex)
print (arr1)
print(type(arr1[0]))
print(arr1.astype(np.complex))
arr2 = np.array(arr2,dtype='complex')
print(arr2)
print(type(arr2[0]))

OUTPUT for above

[25 56 12 85 34 75]
<class 'numpy.int64'>
[25.+0.j 56.+0.j 12.+0.j 85.+0.j 34.+0.j 75.+0.j]
[ 42.+0.j   3.+0.j  86.+0.j  32.+0.j 856.+0.j  46.+0.j]
<class 'numpy.complex128'>

It can be seen that astype changes the type temporally as we do in normal type casting but where as the generic method changes the type permanently

🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.char.chararray.astype.html
numpy.char.chararray.astype — NumPy v2.3 Manual
When casting from complex to float or int. To avoid this, one should use a.real.astype(t). ... Try it in your browser! >>> import numpy as np >>> x = np.array([1, 2, 2.5]) >>> x array([1.
Find elsewhere
🌐
SciPy
docs.scipy.org › doc › numpy-1.6.0 › reference › generated › numpy.ndarray.astype.html
numpy.ndarray.astype — NumPy v1.6 Manual (DRAFT)
May 15, 2011 - NumPy Reference » · Array objects » · The N-dimensional array (ndarray) » · ndarray.astype(t)¶ · Copy of the array, cast to a specified type. Examples · >>> x = np.array([1, 2, 2.5]) >>> x array([ 1. , 2. , 2.5]) >>> x.astype(int) array([1, 2, 2]) numpy.ndarray.dumps ·
🌐
Codecademy
codecademy.com › docs › python:numpy › ndarray › .astype()
Python:NumPy | ndarray | .astype() | Codecademy
July 18, 2024 - The .astype() function in NumPy allows changing the data type of the elements in an array.
🌐
GeeksforGeeks
geeksforgeeks.org › numpy › change-numpy-array-data-type
Change the Data Type of the Given NumPy Array - GeeksforGeeks
The numpy.astype() method is used to change the data type NumPy array from one data type to another.
Published   July 11, 2025
🌐
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.])
🌐
W3Schools
w3schools.com › python › numpy › numpy_data_types.asp
NumPy Data Types
The astype() function creates a copy of the array, and allows you to specify the data type as a parameter. The data type can be specified using a string, like 'f' for float, 'i' for integer etc. or you can use the data type directly like float ...
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: astype() to change dtype of an array | note.nkmk.me
February 4, 2024 - While ndarray supports element-wise ... # [1, 0.1, 'abcXYZ', 1, 0.1, 'abcXYZ'] ... The astype() method of ndarray allows for changing (casting) dtype....
🌐
Medium
medium.com › @mitchparker99 › numpy-array-types-conversions-a19b32026d5e
Numpy: Array Types & Conversions | by Mitchell Parker | Medium
June 11, 2024 - For example: z = np.arange(3, ... for double: np.array([1, 2, 3], dtype='f') np.array([1, 2, 3], dtype='d') To convert the type of an array, the .astype() method is used....
🌐
NumPy
numpy.org › doc › stable › search.html
Search - NumPy v2.4 Manual
Created using Sphinx 7.2.6 · Built with the PyData Sphinx Theme 0.16.1
Top answer
1 of 3
8

They mean if the original array exactly meets the specifications you passed, i.e., has the correct dtype, majorness and is either not a subclass or you set the subok flag, then a copy will be avoided.

The input array is never modified. In your example, the dtypes don't match, so a new array is made regardless.

If you want the data not to be copied use view instead. This will if at all possible reinterpret the data buffer according to your specifications.

x = np.array([1, 2, 2.5])
y = x.view(int)
y
#  array([4607182418800017408, 4611686018427387904, 4612811918334230528])
# y and x share the same data buffer:
y[...] = 0
x
# array([ 0.,  0.,  0.])
2 of 3
2

By default, astype always returns a newly allocated array. If this is set to false, and the dtype, order, and subok requirements are satisfied, the input array is returned instead of a copy.

Notice that the documentation you quoted doesn't mention x being modified at all. In fact, either a brand new array of the desired type is returned, or x is returned unmodified (if possible).

In your case, I believe x doesn't meet the dtype requirement. The documentation doesn't actually describe that requirement at all (so I can understand your confusion), but basically what it means is that the requested dtype (int in this case) must be able to fully represent all values of the original dtype (float in this case). Since you can't cram a float into an int without losing some information, you can't simply pretend that x is an int array.

As such, astype returns a new copy of x, with each value converted to int. It leaves x unmodified, so to get the converted array, you need to check the value returned from astype:

x = np.array([1, 2, 2.5])
y = x.astype(int, copy=False)

print x # Prints array([ 1. ,  2. ,  2.5]), since x hasn't been modified
print y # Prints array([ 1. ,  2. ,  2]), since y is an integer-valued copy of x
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-numpy-matrix-astype
Python | Numpy matrix.astype() - GeeksforGeeks
April 10, 2019 - Example #1 : In this example we can see that how we convert the floating type matrix to int type matrix using matrix.astype() method. ... # import the important module in python import numpy as np # make a matrix with numpy gfg = np.matrix('[1.2, 2.8, 3.1, 4.5]') # applying matrix.astype() method geeks = gfg.astype(int) print(geeks)
🌐
TutorialKart
tutorialkart.com › numpy › numpy-ndarray-astype
NumPy ndarray.astype() Method:
January 29, 2025 - import numpy as np # Creating a float array arr = np.array([1.2, 2.5, 3.8]) # Safe casting to integer (prevents unsafe conversions) try: int_arr = arr.astype(int, casting='safe') print("Converted array:", int_arr) except TypeError as e: print("Error:", e)