[Numpy-discussion] Behavior from a change in dtype?
Mon Sep 7 18:35:52 CDT 2009
On Mon, Sep 7, 2009 at 6:36 PM, Skipper Seabold<firstname.lastname@example.org> wrote:
> Hello all,
> I ran into a problem with some of my older code (since figured out the
> user error). However, in trying to give a simple example that
> replicates the problem I was having, I ran into this.
> In : a = np.array((1.))
> In : a
> Out: array(1.0)
> # the dtype is 'float64'
> In : a.dtype='<i8'
The way I understand it is:
Here you are telling numpy to interpret the existing memory/data in a
different way, which might make sense or not depending on the types,
e.g. I also used this to switch between structured arrays and regular
arrays with compatible memory. However it does not convert the data.
If you want to convert the data to a different type, numpy needs to
create a new array, e.g. with astype
>>> a = np.array((1.))
>>> b = a.astype('<i8')
> In : a
> Out: array(4607182418800017408)
> I've seen some recent threads about handling changes in types, but I
> didn't follow closely, so forgive me if I'm missing something that is
> known. In general, is it just a bad idea to touch the dtype like
> NumPy-Discussion mailing list
More information about the NumPy-Discussion