[Numpy-discussion] asarray issue with type codes
Damian Eads
eads at soe.ucsc.edu
Mon Jul 28 19:50:52 EDT 2008
Hi there,
I ran into a little problem in some type checking code for a C extension
I'm writing. I construct X as a C-long array and then I cast it to a C-int
array Y, however the type code does not change. However, when I try
constructing the array from scratch as a C-int, I get the right type code
(ie 5).
I assumed that when X gets casted to a C-int, no copying should occur but
a new array view should be constructed with the C-int type code. What's
wrong with this logic?
Also note that casting from a C-long (type code 7) to a double to a C-int
returns an array with the right type code, although a double copy occurs.
Damian
# Construct X as a C-long.
In [16]: X=numpy.zeros((10,10),dtype='l')
# Now cast X to a C-int.
In [17]: Y=numpy.asarray(X, dtype='i')
# Check X and Y's data type; they are the same.
In [18]: X.dtype
Out[18]: dtype('int32')
In [19]: Y.dtype
Out[19]: dtype('int32')
# Their type codes are the same.
In [20]: X.dtype.num
Out[20]: 7
In [21]: Y.dtype.num
Out[21]: 7
# Constructing with dtype='i', gives the right type code.
In [22]: Z=numpy.zeros((10,10),dtype='i')
In [23]: Z.dtype
Out[23]: dtype('int32')
In [24]: Z.dtype.num
Out[24]: 5
More information about the NumPy-Discussion
mailing list