4

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.

5
  • Could you add what value type(arr_[0]) returns? Commented Oct 8, 2021 at 7:07
  • @Mahrkeenerh It returns numpy.int32, even I set a dtype np.int. Commented Oct 8, 2021 at 7: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? Commented Oct 8, 2021 at 7: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). Commented Oct 8, 2021 at 7:21
  • display type(arr_[0]) Commented Oct 8, 2021 at 12:06

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
numpy.int32 and numpy.int64 are the types Numpy uses for array scalars of int32 and int64 dtype.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.