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
Possible integer sizes:
| Type | Range |
|---|---|
| int8 | โ128 โ +127 |
| int16 | โ32,768 โ +32,767 |
| int32 | Standard integer |
| int64 | Large integer |
๐ข Float Types
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 |
