NumPy Data Types

🔠 NumPy — Data Types (dtype)

NumPy uses its own optimized data types rather than Python’s built-in types.
These data types allow faster computation and less memory usage, especially when working with large datasets.


📌 Checking the Data Type


 

Output:

int32 (or int64 depending on system)

🧠 Common NumPy Data Types

NumPy TypeMeaning
i / int_Integer
bBoolean
uUnsigned Integer (no negative values)
fFloat
cComplex float
SString
UUnicode string
OPython object
MDateTime
mTime delta

🧮 Integer Types


Possible integer sizes:

TypeRange
int8−128 → +127
int16−32,768 → +32,767
int32Standard integer
int64Large integer

🔢 Float Types


Float types:

TypePrecision
float16half precision
float32single precision
float64double precision

➕ Complex Numbers



🔣 String and Unicode


For Unicode:



✔ Boolean Type



🎯 Converting (Casting) Data Type (astype())

You can convert (cast) an array to another type using astype().



⚠ Casting Rules

✔ Allowed: float → int, int → float
❌ Not allowed automatically: string → numeric (unless values are valid numbers)

Example:



🧠 Memory Efficiency Example


 


📌 Summary Table

CategoryExample
Integer typesint8, int16, int32, int64
Float typesfloat16, float32, float64
Complex numberscomplex64, complex128
Booleanbool_
StringsS (byte string), U (Unicode)
Date/timedatetime64, timedelta64

You may also like...