Python Central HubSign in

Data Analytics with Python / Phase 2: Numerical Computing NumPy

NumPy Data Types (dtypes)

Understand NumPy dtypes, how they control memory and speed, and how to convert types safely for data analytics.

What is a dtype?

A dtype (data type) tells NumPy what kind of values an array contains, such as:

  • integers (int32, int64)
  • floats (float32, float64)
  • booleans (bool)
  • strings (<U...) and bytes (|S...)

Because NumPy uses a single dtype for the entire array, it can store values efficiently and run fast computations. A dtype name is really just a hint about memory layout: a type name (int, float) followed by the number of bits per element. A standard double-precision float takes 8 bytes (64 bits) — hence float64.

diagram How NumPy infers a dtype mermaid
numpy.array walks the input data once and picks the narrowest common dtype that can hold every value.

Checking dtype

Python
import numpy as np
 
arr = np.array([1, 2, 3])
print(arr.dtype)

Common numeric dtypes

Integers

Python
a = np.array([1, 2, 3], dtype=np.int32)
b = np.array([1, 2, 3], dtype=np.int64)
print(a.dtype, b.dtype)

Floats

Python
a = np.array([1.5, 2.0, 3.25], dtype=np.float32)
b = np.array([1.5, 2.0, 3.25], dtype=np.float64)
print(a.dtype, b.dtype)

Memory usage and dtype

Smaller dtypes use less memory. Every element in a float32 array takes 4 bytes; every element in a float64 array takes 8 bytes — double the space for the same number of values.

Python
import numpy as np
 
arr32 = np.ones(1_000_000, dtype=np.float32)
arr64 = np.ones(1_000_000, dtype=np.float64)
 
print("float32 bytes:", arr32.nbytes)
print("float64 bytes:", arr64.nbytes)
sketch Memory layout: int8 vs int64 p5.js
Each block is one byte. A single int64 element takes as much memory as eight int8 elements.

Type conversion

Using .astype()

Python
import numpy as np
 
arr = np.array([1, 2, 3])
arr_f = arr.astype(np.float64)
print(arr_f, arr_f.dtype)

Safe conversion (avoid overflow)

Converting large values into a smaller dtype can overflow.

Python
import numpy as np
 
arr = np.array([300], dtype=np.int16)
print(arr.astype(np.uint8))  # wraps around in many cases

Dtype pitfalls in data analytics

1) Missing values

NumPy numeric arrays can’t store NaN in integer dtype.

Python
import numpy as np
 
# This will upcast to float automatically because of np.nan
arr = np.array([1, 2, np.nan])
print(arr)
print(arr.dtype)

2) Mixed types

If you mix strings and numbers, dtype may become object or strings.

Python
import numpy as np
 
arr = np.array([1, "two", 3])
print(arr)
print(arr.dtype)

Arrays with dtype=object are slower for numerical operations.

Check your understanding

quizCheck yourself
  1. How many bytes does one float64 element take, and one float32 element?

    Show answer

    A — 8 and 4 — The number in the name is bits, so float64 is 64 bits or 8 bytes, and float32 is 4 bytes.

  2. For np.ones(1_000_000, dtype=np.float32), what does nbytes report?

    Show answer

    B — 4000000 — One million elements at 4 bytes each is 4,000,000 bytes. The float64 version would be 8,000,000.

  3. What happens with np.array([1, 2, np.nan])?

    Show answer

    B — NumPy upcasts it to float64 — NaN cannot be stored in an integer dtype, so NumPy upcasts the array to float automatically.

  4. What does arr.astype(np.float64) do to arr?

    Show answer

    B — Returns a new array (a copy) with the new dtype — astype always returns a copy, even when the dtype is unchanged. The original array is untouched.

Next

Continue to: Indexing and Slicing Arrays to learn how to select, filter, and extract parts of arrays.

🧪 Try It Yourself

Exercise 1 – Check and Set dtype

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Convert with astype()

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Memory Cost of a dtype

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did