How do you handle missing and infinite values in NumPy arrays with nan-aware functions and masked arrays?

Quick Overview

Asks how to detect, summarize, replace and mask NaN and infinite values in NumPy arrays. It tests IEEE 754 comparison rules, nan-aware reductions such as nanmean, nan_to_num, boolean masking, and numpy.ma masked arrays, along with their edge cases and costs.

How do you handle missing and infinite values in NumPy arrays with nan-aware functions and masked arrays?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

How can you handle and manipulate arrays with missing or infinite values in NumPy, such as `np.nan`, `np.inf`, nan-aware functions, and masked arrays? Assume a float array where some entries came from failed measurements (`nan`) and others from a division by zero (`inf`). ```hint Start from the float rules Recall what IEEE 754 says about comparing a nan to anything, then check which tools skip a nan and which ones also skip an inf. ``` ### What a Strong Answer Covers - How to detect `nan` and `inf`, and why an equality check does not work - How ordinary reductions and nan-aware reductions differ, including what happens with `inf` and with a slice that is entirely missing - Ways to replace or drop the values, in 1-D and 2-D arrays - What masked arrays add, which dtypes they support, and what they cost - Common pitfalls such as dtype limits and losing the mask ### Follow-up Questions - How would you replace each column's missing values with that column's mean without a Python loop? - How does pandas represent missing values, and how does that differ from plain NumPy for integer columns? - Why does `inf - inf` give `nan`, and how would you find the operation where an `inf` or `nan` first appeared in a long computation?

Overview: Asks how to detect, summarize, replace and mask NaN and infinite values in NumPy arrays. It tests IEEE 754 comparison rules, nan-aware reductions such as nanmean, nan_to_num, boolean masking, and numpy.ma masked arrays, along with their edge cases and costs.

Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

How can you handle and manipulate arrays with missing or infinite values in NumPy, such as np.nan, np.inf, nan-aware functions, and masked arrays?

Assume a float array where some entries came from failed measurements (nan) and others from a division by zero (inf).

What a Strong Answer Covers Guidance

  • How to detect nan and inf , and why an equality check does not work
  • How ordinary reductions and nan-aware reductions differ, including what happens with inf and with a slice that is entirely missing
  • Ways to replace or drop the values, in 1-D and 2-D arrays
  • What masked arrays add, which dtypes they support, and what they cost
  • Common pitfalls such as dtype limits and losing the mask

Follow-up Questions Guidance

  • How would you replace each column's missing values with that column's mean without a Python loop?
  • How does pandas represent missing values, and how does that differ from plain NumPy for integer columns?
  • Why does inf - inf give nan , and how would you find the operation where an inf or nan first appeared in a long computation?