/Curriculum/AdvancedData Cleaning

41. NumPy Masked Arrays (np.ma)Advanced

Handle invalid and missing data safely with masked arrays in np.ma.

14 mins

Concept Overview

The `numpy.ma` module provides masked arrays where invalid or corrupted data points are hidden beneath a boolean mask without discarding them from memory.

Hardware Mental Model

A masked array is placing opaque sticky tape over erroneous cells in a spreadsheet. Calculations automatically treat taped cells as invisible.

Key Concepts (1)Click snippet to load in editor

`ma.masked_where(condition, arr)` hides values satisfying a condition. `ma.masked_invalid(arr)` automatically masks NaNs and Infs.

m = ma.masked_where(arr < 0, arr)

⚠ Common Mistakes & Pitfalls

Avoid these frequent beginner syntax and logic traps
❌ Incorrect:arr.mean() # Normal array will include the -999 error sentinel!
✓ Correct:ma.masked_equal(arr, -999).mean() # Correctly ignores sentinel

Sentinel values like `-999` ruin standard statistics unless masked with `np.ma`.

Pro Tip: Use `np.ma.masked_equal` when dealing with legacy datasets that use numerical sentinels.

📌 Quick Revision

Core takeaway points from this topic
`np.ma`: Masked arrays sub-package.
`ma.masked_equal(arr, val)`: Masks specific sentinel numbers.
`ma.masked_where(cond, arr)`: Masks values matching condition.
`ma.masked_invalid(arr)`: Masks NaNs and Inf values.
Editor: 41. NumPy Masked Arrays (np.ma)
Python ● Ready
Initializing Python IDE...
Ctrl+Enter
Execution Result
Click "RUN CODE" to execute and inspect array state

🎯 Try It Yourself: Practice Challenge

Hands-on Mode

Solve the objective below using NumPy vectorized syntax

Objective:Compute Mean of Masked Array

Create `arr = np.array([10, -999, 30])`, mask `-999` with `ma.masked_equal(arr, -999)`, and print its mean.

Expected Output Target:20.0
Practice Workspace
Python ● Ready
Initializing Python IDE...
Ctrl+Enter
Execution Result
Click "RUN CODE" to execute and inspect array state