NumPy - dtypes

NumPy - dtypes

Envoy_'s photo
Envoy_

Published on Sep 29, 2021

2 min read

Subscribe to my newsletter and never miss my upcoming articles

⚡️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')

⚡️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.

TypeType CodeDescription
int8, uint8i1, u1Signed and unsigned 8-bit (1 byte) integer types
int16, uint16i2, u2Signed and unsigned 16-bit integer types
int32, uint32i4, u4Signed and unsigned 32-bit integer types
int64, uint64i8, u8Signed and unsigned 64-bit integer types
float16f2Half-precision floating point
float32f4 or fStandard single-precision „oating point; compatible with C „oat
float64f8 or dStandard double-precision „oating point; compatible with C double and Python float object
float128f16 or gExtended-precision „oating point
complex64c8, c16Complex numbers represented by two 32 floats
complex128c32Complex numbers represented by two 64 float
complex256c32Complex numbers represented by two 128 float
bool?Boolean type storing True and False values
objectOPython object type; a value can be any Python object
string_SFixed-length ASCII string type (1 byte per character); for example, to create a string dtype with length 10, use 'S10'
unicode_UFixed-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.

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

 
Share this