[Numpy-discussion] Behavior from a change in dtype?

josef.pktd@gmai... josef.pktd@gmai...
Mon Sep 7 18:35:52 CDT 2009

On Mon, Sep 7, 2009 at 6:36 PM, Skipper Seabold<jsseabold@gmail.com> 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 [19]: a = np.array((1.))
> In [20]: a
> Out[20]: array(1.0)
> # the dtype is 'float64'
> In [21]: 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')
>>> b
array(1L, dtype=int64)


> In [22]: a
> Out[22]: 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
> this?
> Best,
> Skipper
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

More information about the NumPy-Discussion mailing list