3

I am confused with the difference between integer types.

For example, here is a numpy.array with dtype of np.int.

>>> arr_ = np.array([1,2], dtype = np.int)

Through the below code, it represents true that int is the same as np.int:

>>> int is np.int
Out[1]: True

However, when I select the first value of the array which is created with dtype of np.int, the below code outputs false.

>>> type(arr_[0]) is int
Out[2]: False

Why does the code output false, not true?

It seems like that dtype = np.int dose not applied on the arr_.

Why np.int dose not applied as a dtype on the array?

I've looked into this, but couldn't get what I need.

  • Could you add what value `type(arr_[0])` returns? – Mahrkeenerh Oct 08 '21 at 07:07
  • @Mahrkeenerh It returns `numpy.int32`, even I set a dtype `np.int`. – Peacepieceonepiece Oct 08 '21 at 07:11
  • 1
    I'm not sure I understand what you're asking for. The question you linked describes how `int` (or `np.int` which is another name for the same type) get converted to `np.int_` when used as an array's `dtype`. That explains the behavior you're seeing exactly. What more do you need to know? – Blckknght Oct 08 '21 at 07:15
  • 2
    The thing you pass as `dtype` is usually not going to be the type object of elements retrieved from the array. After all, you can do `dtype='int32'`, and the string `'int32'` can't be `type(anything)`. – user2357112 Oct 08 '21 at 07:21
  • display `type(arr_[0])` – hpaulj Oct 08 '21 at 12:06

1 Answers1

5

In Python the types int, np.int32 and np.int64 are 3 different types:

  • int is the native Python multiprecision integer type able to represent any integer value (the only limit being available memory). For example 2**128 is a valid int value
  • np.int32 in the int32_t C integer type that can represent values using up to 32 bits, that is values between -2147483648 and 2147483647
  • np.int64 is the int64_t C integer type that can represent values using up to 64 bits, that is values between -9223372036854775808 and 9223372036854775807

And np.int is a (deprecated alias) for the native Python int type, the reason why int is np.int is true. But numpy integer arrays even with dtype=int receive an actual type of np.int32 or np.int64 because they have to be processed by C code so they have to by coerced to a fixed size integer type. If you really need to store true int values, you will have to use dtype=object but operations will no longer be vectorizable.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • 1
    Note that none of these are an actual dtype. dtypes are represented by `numpy.dtype` objects rather than by Python types, because dtypes and types represent different things. A dtype's job is to represent stuff like endianness and structured array padding and other memory layout information, while a Python type object's job is to represent information about inheritance hierarchies and how to dispatch methods and operators. – user2357112 Oct 08 '21 at 07:53
  • 1
    `numpy.int32` and `numpy.int64` are the types Numpy uses for [array scalars](https://numpy.org/doc/stable/reference/arrays.scalars.html) of int32 and int64 dtype. – user2357112 Oct 08 '21 at 07:56