[Numpy-discussion] type conversion question
K.-Michael Aye
kmichael.aye at gmail.com
Thu Apr 18 19:31:36 EDT 2013
I don't understand why sometimes a direct assignment of a new dtype is
possible (but messes up the values), and why at other times a seemingly
harmless upcast (in my potentially ignorant point of view) is not
possible.
So, maybe a direct assignment of a new dtype is actually never a good
idea? (I'm asking), and one should always go the route of newarray=
array(oldarray, dtype=newdtype), but why then sometimes the upcast
provides an error and forbids it and sometimes not?
Examples:
In [140]: slope.read_center_window()
In [141]: slope.data.dtype
Out[141]: dtype('float32')
In [142]: slope.data[1,1]
Out[142]: 10.044398
In [143]: val = slope.data[1,1]
In [144]: slope.data.dtype='float64'
In [145]: slope.data[1,1]
Out[145]: 586.98938070189865
#-----
#Here, the value of data[1,1] has completely changed (and so has the
rest of the array), and no error was given.
# But then...
#----
In [146]: val.dtype
Out[146]: dtype('float32')
In [147]: val
Out[147]: 10.044398
In [148]: val.dtype='float64'
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-148-52a373a41cac> in <module>()
----> 1 val.dtype='float64'
AttributeError: attribute 'dtype' of 'numpy.generic' objects is not writable
=== end of code
So why is there an error in the 2nd case, but no error in the first
case? Is there a logic to it?
Thanks,
Michael
More information about the NumPy-Discussion
mailing list