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:
🧠 Common NumPy Data Types
| NumPy Type | Meaning |
|---|---|
i / int_ |
Integer |
b |
Boolean |
u |
Unsigned Integer (no negative values) |
f |
Float |
c |
Complex float |
S |
String |
U |
Unicode string |
O |
Python object |
M |
DateTime |
m |
Time delta |
🧮 Integer Types
| Type | Range |
|---|---|
| int8 | −128 → +127 |
| int16 | −32,768 → +32,767 |
| int32 | Standard integer |
| int64 | Large integer |
🔢 Float Types
| Type | Precision |
|---|---|
| float16 | half precision |
| float32 | single precision |
| float64 | double 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
| Category | Example |
|---|---|
| Integer types | int8, int16, int32, int64 |
| Float types | float16, float32, float64 |
| Complex numbers | complex64, complex128 |
| Boolean | bool_ |
| Strings | S (byte string), U (Unicode) |
| Date/time | datetime64, timedelta64 |
