Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if a numpy dtype is integral?

How do I check if a numpy dtype is integral? I tried:

issubclass(np.int64, numbers.Integral) 

but it gives False.


Update: it now gives True.

like image 412
Neil G Avatar asked Mar 18 '14 06:03

Neil G


People also ask

How do you check if a value is an integer NumPy?

Use np. integer to check for any instance of either signed or unsigned integers.

What is NumPy Dtype (' O ')?

It means: 'O' (Python) objects. Source. The first character specifies the kind of data and the remaining characters specify the number of bytes per item, except for Unicode, where it is interpreted as the number of characters.

Which function can be used to determine the data type in NumPy?

The astype() function creates a copy of the array, and allows you to specify the data type as a parameter. The data type can be specified using a string, like 'f' for float, 'i' for integer etc. or you can use the data type directly like float for float and int for integer.

What is Dtype (' U32 ') Python?

dtype='<U32' is a little-endian 32 character string. The documentation on dtypes goes into more depth about each of the character. 'U' Unicode string. Several kinds of strings can be converted.


1 Answers

Numpy has a hierarchy of dtypes similar to a class hierarchy (the scalar types actually have a bona fide class hierarchy that mirrors the dtype hierarchy). You can use np.issubdtype(some_dtype, np.integer) to test if a dtype is an integer dtype. Note that like most dtype-consuming functions, np.issubdtype() will convert its arguments to dtypes, so anything that can make a dtype via the np.dtype() constructor can be used.

http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html#specifying-and-constructing-data-types

>>> import numpy as np >>> np.issubdtype(np.int32, np.integer) True >>> np.issubdtype(np.float32, np.integer) False >>> np.issubdtype(np.complex64, np.integer) False >>> np.issubdtype(np.uint8, np.integer) True >>> np.issubdtype(np.bool, np.integer) False >>> np.issubdtype(np.void, np.integer) False 

In a future version of numpy, we will make sure that the scalar types are registered with the appropriate numbers ABCs.

like image 197
Robert Kern Avatar answered Sep 20 '22 11:09

Robert Kern