NumPy Data Types (dtype) & Type Casting (astype)
NumPy Data Types (dtype) & Type Casting (astype)
Because NumPy allocates contiguous memory buffers, it cannot store dynamic arbitrary types in individual cells. Instead, it relies on fixed-width, low-level C-compatible data types known as dtypes. Understanding dtypes is vital for preventing integer overflows and optimizing memory consumption.
1. Overview of NumPy Data Types
NumPy offers granular control over bit-width and signedness:
| Category | Dtype Names | Bit Width | Permitted Range / Value |
|---|---|---|---|
| Integer (Signed) | int8, int16, int32, int64 | 8, 16, 32, 64 bits | Negative and positive whole numbers (int8: -128 to 127) |
| Integer (Unsigned) | uint8, uint16, uint32, uint64 | 8, 16, 32, 64 bits | Non-negative integers (uint8: 0 to 255; ideal for pixels!) |
| Floating Point | float16, float32, float64 | 16, 32, 64 bits | Real numbers with fractional decimals |
| Complex | complex64, complex128 | 64, 128 bits | Numbers with real and imaginary components ($3 + 4j$) |
| Boolean | bool or bool_ | 8 bits (1 byte) | True or False |
| String / Unicode | str_ or U<N> | Variable | Fixed-width Unicode characters |
2. Type Casting with astype()
To convert an existing array from one data type to another, use the astype() method.
astype() always creates and returns a NEW deep copy of the array in memory; it never mutates the original array in-place!3. The Danger of Integer Overflow
When using small fixed-width integer types (like int8 or uint8), exceeding the maximum boundary value wraps around without warning:
In financial calculations or large aggregates, always use int64 or float64 to avoid silent arithmetic overflow!
Multiple Choice Questions
1. What does the 'astype()' method do to an existing NumPy array?
A. Modifies the array in-place without copying B. Creates and returns a brand new copy of the array converted to the requested dtype C. Deletes all negative numbers D. Sorts the array Answer: B Explanation: astype() is a non-destructive method that allocates a new memory buffer and casts the values into the target dtype.
2. What is the value range of an unsigned 8-bit integer ('uint8') in NumPy?
A. -128 to 127 B. 0 to 255 C. 0 to 65,535 D. -255 to 255 Answer: B Explanation: An unsigned 8-bit integer (2^8 = 256 states) represents non-negative numbers strictly from 0 to 255.
3. What happens when you cast a floating-point number 7.89 into an integer using 'astype(int)'?
A. It rounds up to 8 B. It truncates the decimal portion toward zero, becoming 7 C. It raises a TypeError D. It returns NaN Answer: B Explanation: In C and NumPy, integer casting truncates fractional decimals toward zero rather than rounding.
4. What will happen if you add 1 to a uint8 array containing the value 255: 'np.array([255], dtype=np.uint8) + 1'?
A. Returns array([256]) B. Triggers an OverflowError exception C. Wraps around to 0 due to 8-bit integer overflow D. Converts into float Answer: C Explanation: Fixed-width integer types experience modular wrap-around overflow; 255 + 1 in 8-bit unsigned arithmetic wraps back to 0.
5. Which of the following data types occupies the least amount of memory per element?
A. np.float64 B. np.int32 C. np.int8 D. np.complex128 Answer: C Explanation: np.int8 consumes exactly 1 byte (8 bits) per element, compared to 4 bytes for int32 and 8 bytes for float64.
Inspecting Array Attributes (shape, ndim, size, itemsize)
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.