DEV Community

Cover image for NumPy - dtypes
Envoy-VC
Envoy-VC

Posted on

NumPy - dtypes

⚡️What is a dtype

The data type or dtype is a special object containing the information (or metadata,
data about data) the ndarray needs to interpret a chunk of memory as a particular
type of data:

arr = np.array([1,2,3],dtype="float64")

print(arr.dtype) # dtype('float64')
Enter fullscreen mode Exit fullscreen mode

⚡️Why dtypes

dtypes are a source of NumPy’s flexibility for interacting with data coming from other
systems. In most cases they provide a mapping directly onto an underlying disk or
memory representation, which makes it easy to read and write binary streams of data
to disk

⚡️Types of dtypes

Don’t worry about memorizing the NumPy dtypes, especially if
you’re a new user. It’s often only necessary to care about the general
kind of data you’re dealing with, whether floating point, complex,
integer, boolean, string, or general Python object. When you need
more control over how data are stored in memory and on disk,
especially large datasets, it is good to know that you have control
over the storage type.

Type Type Code Description
int8, uint8 i1, u1 Signed and unsigned 8-bit (1 byte) integer types
int16, uint16 i2, u2 Signed and unsigned 16-bit integer types
int32, uint32 i4, u4 Signed and unsigned 32-bit integer types
int64, uint64 i8, u8 Signed and unsigned 64-bit integer types
float16 f2 Half-precision floating point
float32 f4 or f Standard single-precision „oating point; compatible with C „oat
float64 f8 or d Standard double-precision „oating point; compatible with C double and Python float object
float128 f16 or g Extended-precision „oating point
complex64 c8, c16 Complex numbers represented by two 32 floats
complex128 c32 Complex numbers represented by two 64 float
complex256 c32 Complex numbers represented by two 128 float
bool ? Boolean type storing True and False values
object O Python object type; a value can be any Python object
string_ S Fixed-length ASCII string type (1 byte per character); for example, to create a string dtype with length 10, use 'S10'
unicode_ U Fixed-length Unicode type (number of bytes platform specific); same specification semantics as string_ (e.g., 'U10')

Note -

It’s important to be cautious when using the numpy.string_
type,as string data in NumPy is fixed size and may 
truncate input without warning. pandas has more intuitive 
out-of-the-box behavior on non-numeric data.
Enter fullscreen mode Exit fullscreen mode

⚡️Join my Discord for Updates and downloads for future materials

Discussion (0)