About the copy
parameter:
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.
So it's conditional.
In [540]: x=np.arange(10)
In [542]: x.dtype
Out[542]: dtype('int32')
In [543]: z=x.astype('float32',copy=False)
In [544]: z
Out[544]: array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.], dtype=float32)
In [545]: x.__array_interface__
Out[545]:
{'data': (188221848, False),
'descr': [('', '<i4')],
'shape': (10,),
'strides': None,
'typestr': '<i4',
'version': 3}
In [546]: z.__array_interface__
Out[546]:
{'data': (191273640, False),
'descr': [('', '<f4')],
'shape': (10,),
'strides': None,
'typestr': '<f4',
'version': 3}
z
has different memory location.
The accepted answer in your link appears to work
In [549]: z=x.view('float32')
In [550]: z[:]=x
In [551]: z
Out[551]: array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.], dtype=float32)
In [552]: x
Out[552]:
array([ 0, 1065353216, 1073741824, 1077936128, 1082130432,
1084227584, 1086324736, 1088421888, 1090519040, 1091567616])
In [553]: z
Out[553]: array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.], dtype=float32)
In [555]: x.__array_interface__
Out[555]:
{'data': (188221848, False),
'descr': [('', '<i4')],
'shape': (10,),
'strides': None,
'typestr': '<i4',
'version': 3}
In [556]: z.__array_interface__
Out[556]:
{'data': (188221848, False),
'descr': [('', '<f4')],
'shape': (10,),
'strides': None,
'typestr': '<f4',
'version': 3}
This works because z
shares memory with x
, but with a different dtype
. When copied from x
to z
, they are converted to match the new dtype. Memory locations is preserved. However I can't guarantee that there wasn't a temporary buffer.
In case it isn't clear, conversion form int32
to float32
requires a change in the underlying bytes. The bit representation of integers is different from that of floats.
In [594]: np.array(1, 'int32').tobytes()
Out[594]: b'\x01\x00\x00\x00'
In [595]: np.array(1, 'float32').tobytes()
Out[595]: b'\x00\x00\x80?'