numpy
matrix
NaN
Python
data science

Create a NumPy array or matrix filled with NaN

Codemia Knowledge Hub ·

Use np.full(shape, np.nan) to create a NumPy array filled with NaN, for example np.full((4, 3), np.nan, dtype=np.float64). NaN only exists in floating-point arrays, so never start from an integer dtype.

NaN-filled arrays are a common way to represent missing values, preallocate partially known tables, or build rolling buffers that stay empty until measurements arrive. What matters most is choosing the dtype, how you fill values later, and how you compute statistics over the gaps.

np.full: the clearest initialization

python
1import numpy as np
2
3rows, cols = 4, 3
4m = np.full((rows, cols), np.nan, dtype=np.float64)
5
6print(m)
7print(m.dtype)

Shape, fill value and dtype are all explicit in one line, which makes this the best choice for general-purpose code.

np.empty plus fill when allocation is a separate step

When a pipeline allocates and fills in separate steps, np.empty followed by .fill() works:

python
1import numpy as np
2
3m = np.empty((1000, 1000), dtype=np.float32)
4m.fill(np.nan)
5
6print(np.isnan(m).all())

It can suit performance-tuned code, but np.full is clearer for most applications.

Why the dtype must be float

NaN is a floating-point concept. Storing it in an integer array gives conversion errors or invalid behavior:

python
1import numpy as np
2
3arr = np.zeros((2, 2), dtype=np.int32)
4# arr[0, 0] = np.nan  # invalid for integer dtype
5
6arr_float = arr.astype(np.float64)
7arr_float[0, 0] = np.nan
8print(arr_float)

If missing values are part of the data model, start with a float dtype rather than converting later.

Filling known values incrementally

Initialize everything as missing, then write observed values as they arrive:

python
1import numpy as np
2
3m = np.full((3, 3), np.nan)
4m[0, 1] = 10.2
5m[2, 0] = 5.7
6
7print(m)

This beats using zero as a placeholder when zero is also a valid measurement, because NaN keeps missingness explicit.

NaN-aware aggregation functions

Ordinary NumPy reductions propagate NaN, so one missing value can turn the whole result into NaN. To compute statistics over observed values only, use the nan-aware functions:

python
1import numpy as np
2
3m = np.array([
4    [1.0, np.nan, 3.0],
5    [np.nan, 5.0, 6.0]
6])
7
8print(np.nanmean(m))
9print(np.nanmax(m, axis=0))

Pairing the array with an observation mask

An explicit boolean mask next to the values helps when you must tell "missing" apart from "present but zero" in later reporting or model explanations:

python
1import numpy as np
2
3values = np.full((2, 4), np.nan)
4observed = np.zeros((2, 4), dtype=bool)
5
6values[0, 0] = 1.5
7observed[0, 0] = True
8
9print(values)
10print(observed)

Mistakes with NaN arrays

  • Creating an integer array and expecting NaN assignment to work. Choose a float dtype up front.
  • Using ordinary reductions such as mean or max and forgetting that NaN affects the result. nanmean, nanmax and related functions are often the right tools.
  • Serializing arrays without checking that NaN values survived the round trip, which matters when moving between NumPy, CSV, pandas and model pipelines.
  • Replacing NaN with zero without meaning to change what the data says.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design