
Hi,
Can someone please explain to me this oddity?
In [1]: import numpy as n
In [8]: a = n.array((1,2,3), 'i')
In [9]: type(a[0]) Out[9]: <type 'numpy.int32'>
In [10]: type(a[0]) == n.int32 Out[10]: False
When I create an array with 'int', 'int32' etc it works fine
What is the type of 'i' and what is n.int0?
Thanks, Suchindra

2008/7/18 Suchindra Sandhu suchindra@gmail.com:
Can someone please explain to me this oddity?
In [1]: import numpy as n
In [8]: a = n.array((1,2,3), 'i')
In [9]: type(a[0]) Out[9]: <type 'numpy.int32'>
There's more than one int32 type lying around. Rather, compare *dtypes*:
In [19]: a.dtype == np.int32 Out[19]: True
Regards Stéfan

Hi Stéfan,
Is that the recommended way of checking the type of the array? Ususally for type checkin, I use the isinstance built-in in python, but I see that will not work in this case. I must admit that I am a little confused by this. Why is type different from dtype?
Thanks, Suchindra
On Fri, Jul 18, 2008 at 7:10 PM, Stéfan van der Walt stefan@sun.ac.za wrote:
2008/7/18 Suchindra Sandhu suchindra@gmail.com:
Can someone please explain to me this oddity?
In [1]: import numpy as n
In [8]: a = n.array((1,2,3), 'i')
In [9]: type(a[0]) Out[9]: <type 'numpy.int32'>
There's more than one int32 type lying around. Rather, compare *dtypes*:
In [19]: a.dtype == np.int32 Out[19]: True
Regards Stéfan _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion

2008/7/21 Suchindra Sandhu suchindra@gmail.com:
Is that the recommended way of checking the type of the array? Ususally for type checkin, I use the isinstance built-in in python, but I see that will not work in this case. I must admit that I am a little confused by this. Why is type different from dtype?
Data-types contain additional information needed to lay out numerical types in memory, such as byte-order and bit-width. Each data-type has an associated Python type, which tells you the type of scalars in an array of that dtype. For example, here are two NumPy data-types that are not equal:
In [6]: d1 = np.dtype(int).newbyteorder('>') In [7]: d2 = np.dtype(int).newbyteorder('<')
In [8]: d1.type Out[8]: <type 'numpy.int32'>
In [9]: d2.type Out[9]: <type 'numpy.int32'>
In [10]: d1 == d2 Out[10]: False
I don't know why there is more than one int32 type (I would guess it has something to do with the way types are detected upon build; maybe Robert or Travis could tell you more).
Regards Stéfan

On Mon, Jul 21, 2008 at 3:37 PM, Stéfan van der Walt stefan@sun.ac.za wrote:
2008/7/21 Suchindra Sandhu suchindra@gmail.com:
Is that the recommended way of checking the type of the array? Ususally
for
type checkin, I use the isinstance built-in in python, but I see that
will
not work in this case. I must admit that I am a little confused by this.
Why
is type different from dtype?
Data-types contain additional information needed to lay out numerical types in memory, such as byte-order and bit-width. Each data-type has an associated Python type, which tells you the type of scalars in an array of that dtype. For example, here are two NumPy data-types that are not equal:
In [6]: d1 = np.dtype(int).newbyteorder('>') In [7]: d2 = np.dtype(int).newbyteorder('<')
In [8]: d1.type Out[8]: <type 'numpy.int32'>
In [9]: d2.type Out[9]: <type 'numpy.int32'>
In [10]: d1 == d2 Out[10]: False
I don't know why there is more than one int32 type (I would guess it has something to do with the way types are detected upon build; maybe Robert or Travis could tell you more).
They correspond to two C types of the same size, int and long. On 64 bit systems you should have two int64 types, long and longlong.
In [1]: dtype('i').name Out[1]: 'int32'
In [2]: dtype('l').name Out[2]: 'int32'
Chuck

Thanks Everyone.
On Mon, Jul 21, 2008 at 6:25 PM, Charles R Harris charlesr.harris@gmail.com wrote:
On Mon, Jul 21, 2008 at 3:37 PM, Stéfan van der Walt stefan@sun.ac.za wrote:
2008/7/21 Suchindra Sandhu suchindra@gmail.com:
Is that the recommended way of checking the type of the array? Ususally
for
type checkin, I use the isinstance built-in in python, but I see that
will
not work in this case. I must admit that I am a little confused by this.
Why
is type different from dtype?
Data-types contain additional information needed to lay out numerical types in memory, such as byte-order and bit-width. Each data-type has an associated Python type, which tells you the type of scalars in an array of that dtype. For example, here are two NumPy data-types that are not equal:
In [6]: d1 = np.dtype(int).newbyteorder('>') In [7]: d2 = np.dtype(int).newbyteorder('<')
In [8]: d1.type Out[8]: <type 'numpy.int32'>
In [9]: d2.type Out[9]: <type 'numpy.int32'>
In [10]: d1 == d2 Out[10]: False
I don't know why there is more than one int32 type (I would guess it has something to do with the way types are detected upon build; maybe Robert or Travis could tell you more).
They correspond to two C types of the same size, int and long. On 64 bit systems you should have two int64 types, long and longlong.
In [1]: dtype('i').name Out[1]: 'int32'
In [2]: dtype('l').name Out[2]: 'int32'
Chuck
Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
participants (3)
-
Charles R Harris
-
Stéfan van der Walt
-
Suchindra Sandhu