Array Data Types (dtypes)
Reviewed & published by Brayan K
A dtype is the data type that describes what kind of value every element of a NumPy array holds and how much memory it uses — because the ndarray is homogeneous, that single dtype applies to the entire array at once.
Part of the free NumPy course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll meet the common dtypes, learn to set one at creation, convert between them with .astype(), and avoid the overflow and precision traps that catch beginners.
What You'll Learn in This Lesson
- What a dtype is and why it matters
- Common dtypes: int32, int64, float64, bool, complex
- Setting dtype= when you create an array
- Reading the .dtype attribute
- Converting types with .astype()
- Overflow, precision, and memory (itemsize, nbytes)
1 What Is a dtype?
A dtype tells NumPy what each element is and how many bytes it occupies. Common ones include int64 (whole numbers), float64 (decimals), bool (True/False), and complex128 (complex numbers). Read it with the .dtype attribute.
import numpy as np
ints = np.array([1, 2, 3])
floats = np.array([1.0, 2.5, 3.0])
flags = np.array([True, False, True])
print(ints.dtype) # int64
print(floats.dtype) # float64
print(flags.dtype) # bool
# ✅ Expected output:
# int64
# float64
# boolExpected output: int64, float64, and bool — NumPy infers the dtype from the values you supply.
2 Setting the dtype at Creation
You don't have to accept the inferred type. Pass dtype= to any creation function to force a specific type. This is useful for saving memory (a smaller int) or guaranteeing decimals (a float).
import numpy as np
a = np.array([1, 2, 3], dtype=np.float64)
print(a) # [1. 2. 3.]
print(a.dtype) # float64
b = np.zeros(3, dtype=np.int32)
print(b) # [0 0 0]
print(b.dtype) # int32
# ✅ Expected output:
# [1. 2. 3.]
# float64
# [0 0 0]
# int32Expected output: the float array [1. 2. 3.] with dtype float64, and an int32 zeros array.
3 Converting Types with .astype()
To change an existing array's type, call .astype(). It returns a brand-new array; the original is untouched. Converting floats to ints truncates toward zero (it drops the decimal part, it does not round).
import numpy as np
prices = np.array([10.9, 20.2, 30.7])
as_int = prices.astype(np.int32)
print(as_int) # [10 20 30] -> decimals are dropped, not rounded
back_to_float = as_int.astype(np.float64)
print(back_to_float) # [10. 20. 30.]
print(prices) # [10.9 20.2 30.7] -> original is unchanged
# ✅ Expected output:
# [10 20 30]
# [10. 20. 30.]
# [10.9 20.2 30.7]Expected output: [10 20 30] (decimals truncated), [10. 20. 30.], and the original [10.9 20.2 30.7] still intact.
4 Overflow, Precision & Memory
Small integer dtypes save memory but have limits. An int8 only holds -128 to 127, so going past the limit wraps around. You can also check how much space an array uses with .itemsize (bytes per element) and .nbytes (total bytes).
import numpy as np
small = np.array([127], dtype=np.int8)
print(small + 1) # [-128] -> overflow wraps around
big = np.array([1, 2, 3], dtype=np.int64)
print(big.itemsize) # 8 -> bytes per element
print(big.nbytes) # 24 -> 3 elements * 8 bytes
# ✅ Expected output:
# [-128]
# 8
# 24Expected output: [-128] from the overflow, then 8 bytes per int64 element and 24 bytes total.
🎯 YOUR TURN: Fill in the Blanks
Replace each ___ so the program creates a float array and converts it to integers.
import numpy as np
data = np.array([1, 2, 3], dtype=np.___) # fill in the type for decimals -> float64
print(data) # [1. 2. 3.]
ints = data.___(np.int32) # fill in the method that converts dtype
print(ints) # [1 2 3]
print(ints.___) # fill in the attribute that shows the dtype -> int32Expected output: [1. 2. 3.], [1 2 3], then int32. (Answers: float64, astype, dtype.)
⚠️ Common Errors & Quick Reference
❌ My integers became negative out of nowhere
A narrow dtype like int8 overflowed past its limit and wrapped around.
✅ Fix: create the array with a wider dtype such as np.int64.
❌ astype dropped my decimals
Casting a float to an int truncates toward zero — it does not round.
✅ Fix: round first with np.round(arr).astype(np.int64) if you need rounding.
| Task | Code |
|---|---|
| Check the dtype | arr.dtype |
| Set dtype at creation | np.array([1,2], dtype=np.float64) |
| Convert dtype | arr.astype(np.int32) |
| Total bytes used | arr.nbytes |
🏆 Mini Challenge: Shrink the Memory
Make a float64 array, convert it to int16, and compare how many bytes each uses.
import numpy as np
big = np.array([1, 2, 3, 4], dtype=np.float64)
print(big.nbytes) # 32 -> 4 elements * 8 bytes
small = big.astype(np.int16)
print(small) # [1 2 3 4]
print(small.nbytes) # 8 -> 4 elements * 2 bytes
# ✅ Expected output:
# 32
# [1 2 3 4]
# 8❓ Frequently Asked Questions
Lesson complete — you control your dtypes!
You now know the common dtypes, how to set one at creation, how to convert with .astype(), and how to avoid overflow while keeping an eye on memory with itemsize and nbytes.
Practice quiz
What is a dtype in NumPy?
- The shape of the array
- The data type describing each element and its byte size
- The number of dimensions
- The array's variable name
Answer: The data type describing each element and its byte size. A dtype describes what each element is (int64, float64, bool, ...) and how many bytes it uses.
What dtype does np.array([1.0, 2.0]) get by default?
- int64
- float32
- bool
- float64
Answer: float64. Decimal literals make NumPy infer float64.
How do you force a specific dtype when creating an array?
- Pass the dtype= keyword
- Call .shape()
- Use np.cast
- It cannot be set at creation
Answer: Pass the dtype= keyword. Every creation function accepts dtype=, e.g. np.zeros(4, dtype=np.int32).
Which method converts an existing array to a new dtype?
- .dtype()
- .reshape()
- .astype()
- .convert()
Answer: .astype(). .astype(newtype) returns a new array cast to the requested dtype.
What does np.array([1.9, 2.1]).astype(np.int32) return?
- [2, 2]
- [1, 2]
- [2, 3]
- [1, 3]
Answer: [1, 2]. Float-to-int truncates toward zero (drops the decimals), giving [1, 2].
What does np.array([127], dtype=np.int8) + 1 return?
- [128]
- [0]
- [127]
- [-128]
Answer: [-128]. int8 maxes at 127, so adding 1 overflows and wraps to -128.
What is the itemsize of an int64 array element?
- 8
- 4
- 2
- 16
Answer: 8. int64 uses 64 bits = 8 bytes per element.
What does .nbytes report for np.array([1, 2, 3], dtype=np.int64)?
- 3
- 8
- 24
- 64
Answer: 24. 3 elements * 8 bytes each = 24 bytes total.
Why might a large integer suddenly turn negative?
- NumPy rounds randomly
- Integer overflow wrapped a narrow dtype past its limit
- astype always negates
- The array became a float
Answer: Integer overflow wrapped a narrow dtype past its limit. A fixed-width int dtype like int8 wraps around when a value exceeds its range.
How can you avoid integer overflow for large values?
- Use a narrower dtype like int8
- Use a wider dtype like int64
- Convert to bool
- Reduce the array size
Answer: Use a wider dtype like int64. A wider dtype such as int64 holds a much larger range of values.
Continue this course
- Previous: Images as Arrays: Shape, uint8 & Channels
- Next: Array Attributes: shape, ndim, size — Inspect the structure of any array with its key attributes
- Quick reference: NumPy cheat sheet
Frequently asked questions
What is a dtype in NumPy?
A dtype (data type) describes what kind of value every element of an array holds and how many bytes it uses — for example int64, float64, bool, or complex128. Because an ndarray is homogeneous, the whole array shares one dtype.
How do I set the dtype when creating an array?
Pass the dtype keyword to the creation function, for example np.array([1, 2, 3], dtype=np.float64) or np.zeros(4, dtype=np.int32). The array will then store every element in that exact type.
How do I convert an array to a different dtype?
Use the .astype() method, which returns a new array with the values cast to the new type. For example arr.astype(np.float64) turns ints into floats, and arr.astype(np.int32) truncates floats toward zero.
Why did my large integer turn into a negative number?
That is integer overflow. A fixed-width integer dtype like int8 can only hold values from -128 to 127, so a value past the limit wraps around. Use a wider dtype such as int64 to avoid it.