If I wanted to change the data type of a numpy array permanently, is reassignment the best way?
Here’s an example to illustrate the syntax:
import numpy as np
x = np.array([1],dtype='float')
x = x.view(dtype=('x','float"))
However, I’d prefer to change change the data type “in place”.
Is there any way to change the dtype of an array in-place? Or is it better to so something similar to this?:
x = x.view(dtype=('x',"float")).copy()
There is no “permanent” dtype, really.
Numpy arrays are basically just a way of viewing a memory buffer.
Views, in the numpy sense, are just a different way of slicing and dicing the same memory buffer without making a copy.
Keep in mind, though, that this is giving you low-level control over the way that the memory buffer is interpreted.
For example:
This yields:
So, we’re interpreting the underlying bits of the original memory buffer as floats, in this case.
If we wanted to make a new copy with the ints recasted as floats, we’d use
x.astype(np.float).The reason views (of dtypes… Slicing returns a view as well, though that’s a separate topic.) are so useful, is that you can do some really nice tricks without having to duplicate things in memory.
For example, if you wanted to convert floats to ints in place (without duplicating memory), you can use a couple of tricks with views to do this. (Based on @unutbu’s answer on this question.)
Similarly, you can do various low-level bit-twiddling:
To answer your question, “better” is all relative. Do you want a copy, or do you want to view the same memory buffer in a different way?
As a side note,
astypealways makes a copy, regardless of the “input” and “output” dtypes. It’s often what people actually want when they refer toview. (e.g. if you want to convert between ints and floats, useastype, not view, unless you need to micro-manage memory usage.)