Thursday , December 3 2026
Numpy Datatypes

Numpy Datatypes

Updated: 28 November 2026Level: BeginnerReading time: about 7 minutesNumPy 1.26+ / 2.x

Every NumPy array has a single data type (dtype) shared by all its elements. Choosing the right dtype affects memory use, speed and accuracy.

Prerequisites

Creating NumPy Arrays.

Checking the dtype

Example
import numpy as np
print(np.array([1, 2, 3]).dtype)
print(np.array([1.5, 2.0]).dtype)
print(np.array(['a', 'bc']).dtype)
print(np.array([True, False]).dtype)

Explanation: On Windows with older NumPy versions the default integer may show as int32. <U2 means Unicode strings up to 2 characters.

Output
int64
float64
<U2
bool

Common Data Types

dtype Description Range / Notes
int8, int16, int32, int64 Signed integers int8: -128 to 127
uint8 Unsigned 8-bit 0 to 255, used for images
float16, float32, float64 Decimals float32 is common in deep learning
bool True/False
complex128 Complex numbers
str_ / U Text Fixed length
object Any Python object Slow, avoid

Setting the dtype

Example
a = np.array([1, 2, 3], dtype=np.float32)
b = np.zeros(3, dtype='int16')
print(a, a.dtype)
print(b, b.dtype)
Output
[1. 2. 3.] float32
[0 0 0] int16

Converting with astype()

Example
x = np.array([1.7, 2.2, -3.9])
print(x.astype(int))
print(np.array(['10', '20']).astype(float))

Explanation: astype() returns a new array. Converting float to int cuts off the decimal (towards zero); it does not round.

Output
[ 1  2 -3]
[10. 20.]

Mixed Types Are Upcast

Example
print(np.array([1, 2.5]).dtype)
print(np.array([1, 'a']))

Explanation: NumPy picks a type that can hold all values, so an int and a float become float, and anything with a string becomes a string.

Output
float64
['1' 'a']

Overflow

Example
a = np.array([250], dtype=np.uint8)
print(a + 10)
Output
[4]
Note: Small integer types wrap around silently when they overflow (250 + 10 = 260 → 4). Convert to a larger type before maths on image data.

Memory Usage

Example
a = np.zeros(1000, dtype=np.float64)
b = np.zeros(1000, dtype=np.float32)
print(a.nbytes, b.nbytes, a.itemsize)
Output
8000 4000 8

Common Errors

Problem Cause and fix
ValueError: could not convert string to float A string in the array is not a number; clean it before astype(float).

Practice Exercises

  1. Create a uint8 array and check what happens at 256.
  2. Convert an array of strings of numbers to float and find the mean.

Interview Questions

Q: Why use float32 instead of float64 in deep learning?
Half the memory and faster on GPUs, with enough precision for training.

Q: What does astype return?
A new array (copy) with the new dtype.

Summary

Check dtype with .dtype, set it with dtype=, convert with astype(), and watch for overflow in small integer types.

Next: Inspect an Array · Random Arrays

About Machine Learning

Check Also

Exploratory Data Analysis (EDA) in Python: Step-by-Step Checklist

A step-by-step EDA checklist in pandas with tested outputs: shape, missing values, describe, value counts, groupby, correlations and charts.