IEEE NaN (Not a Number) propagates through math. NumPy provides np.isnan, np.nanmean, and masking patterns for missing numeric data—common after loading real datasets from data science pipelines.
Detecting NaN
import numpy as np
a = np.array([1.0, np.nan, 3.0])
print(np.isnan(a))
print(a[~np.isnan(a)])
NaN-aware aggregations
np.nanmean,np.nansum,np.nanstdnp.where(np.isnan(a), fill, a)— impute- Regular
mean()returns NaN if any NaN present
Pandas bridge
Pandas uses NaN for missing floats; df.to_numpy() may expose NaN in ndarray columns—handle before sklearn.
Important interview questions and answers
- Q: NaN == NaN?
A: False—use np.isnan, not equality checks. - Q: nanmean vs mean with mask?
A: nanmean is concise; boolean mask gives full control.
Self-check
- Compute mean ignoring NaN values.
- Filter out NaN from an array.
Tip: Never test x == np.nan—use np.isnan.
Interview prep
- isnan?
Correct way to detect NaN—equality fails.
- nanmean?
Aggregate ignoring missing values.