I am confused about the copy attribution of numpy.astype.
I check out the material in a link; it said:
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.
Does it mean that it will change the original value of a ndarray object?
Like:
x = np.array([1, 2, 2.5])
x.astype(int, copy=False)
But it seems that x still is the original value array([ 1., 2., 2.5]).
What is the explanation?
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.])