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
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:
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:
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:
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:
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:
Mistakes with NaN arrays
- Creating an integer array and expecting
NaNassignment to work. Choose a float dtype up front. - Using ordinary reductions such as
meanormaxand forgetting thatNaNaffects the result.nanmean,nanmaxand related functions are often the right tools. - Serializing arrays without checking that
NaNvalues survived the round trip, which matters when moving between NumPy, CSV, pandas and model pipelines. - Replacing
NaNwith zero without meaning to change what the data says.
Related reading
- Create Pandas DataFrame from a string
- Create Spark DataFrame in Spark Streaming from JSON Message on Kafka
- Creating a learner object for Bayesian optimization using the mlr and mlrMBO packages example with a neural network model using the nnet package in R
- Creating a Pandas DataFrame from a Numpy array How do I specify the index column and column headers?
- Creating a dictionary from a csv file?
- Creating a JSON response using Django and Python
- Creating an empty Pandas DataFrame, and then filling it
- Cross validation and model selection
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.