Explain the concept and use of masked arrays (np.ma) in NumPy.

Quick Overview

Asks how NumPy masked arrays pair data with a boolean mask to handle missing or invalid entries. Tests whether you know how masks behave in aggregations and arithmetic, how to build and remove them, and when NaN-aware functions or pandas are the better choice.

Explain the concept and use of masked arrays (np.ma) in NumPy.

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Explain the concept and use of masked arrays (`np.ma`) in NumPy. ```hint Compare with NaN Think about the usual way to mark missing values in NumPy. What does it require of the dtype, and what happens to that marker inside an ordinary `sum` or `mean`? ``` ### What a Strong Answer Covers - What a masked array stores and what a `True` mask entry means - How masks behave in aggregations and in elementwise arithmetic - How to build masks from conditions, sentinel values or NaN, and how to get a plain array back - Trade-offs against NaN-based handling and pandas, including speed and functions that drop the mask ### Follow-up Questions - You pass a masked array to a library function that calls `np.asarray` on its input. What happens to the masked values? - How does `np.ma.log` treat zero and negative inputs, and when is that useful? - An integer climate grid uses -9999 as a missing-value sentinel. How would you compute the mean of each row? - When would you choose a masked array over `np.nanmean` and the other NaN-aware functions on a float array?

Overview: Asks how NumPy masked arrays pair data with a boolean mask to handle missing or invalid entries. Tests whether you know how masks behave in aggregations and arithmetic, how to build and remove them, and when NaN-aware functions or pandas are the better choice.

PracHub logo
General
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Explain the concept and use of masked arrays (np.ma) in NumPy.

What a Strong Answer Covers Guidance

  • What a masked array stores and what a True mask entry means
  • How masks behave in aggregations and in elementwise arithmetic
  • How to build masks from conditions, sentinel values or NaN, and how to get a plain array back
  • Trade-offs against NaN-based handling and pandas, including speed and functions that drop the mask

Follow-up Questions Guidance

  • You pass a masked array to a library function that calls np.asarray on its input. What happens to the masked values?
  • How does np.ma.log treat zero and negative inputs, and when is that useful?
  • An integer climate grid uses -9999 as a missing-value sentinel. How would you compute the mean of each row?
  • When would you choose a masked array over np.nanmean and the other NaN-aware functions on a float array?