I found a convoluted way to do an in-place type conversion
https://stackoverflow.com/a/4396247/901925
In that example the conversion was from 'int32' to 'float32'. Adapted to our case:
Initial x
with a large enough string size:
In [128]: x=np.array(['0','1'],dtype='S4')
In [129]: x.__array_interface__['data']
Out[129]: (173756800, False) # data buffer location
now make a view, and copy values from x
to the view:
In [130]: y=x.view(int)
In [131]: y[:]=x
Same data buffer location (same for y
)
In [132]: x.__array_interface__['data']
Out[132]: (173756800, False)
Now y
is two ints:
In [133]: y
Out[133]: array([0, 1])
x
, which is still S4
, looks at these bytes in a different way:
In [134]: x
Out[134]:
array([b'', b'\x01'],
dtype='|S4')
So it is possible to perform data type conversions in-place, if byte sizes match, but it is an advanced operation. Both the person asking that question and the one answering it are numpy
experts.
And astype,copy=False
is mentioned in another answer, but fails for the same reason as here - it can't perform the conversion without changing the original array.