Compute NaN-aware column means of a NumPy array

Quick Overview

Compute the mean of each column of a 2-D NumPy array while ignoring NaN entries. It tests axis-based reductions, how np.nanmean differs from np.mean, and how to handle columns that are entirely missing without a misleading result or an unexpected warning.

Compute NaN-aware column means of a NumPy array

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Given a 2-D `np.ndarray` of floats that contains some `np.nan` entries, write `column_means_ignore_nan(a)` that returns a 1-D array with the mean of each column, ignoring the missing values. Reduce along `axis=0`, so the result has one entry per column. Skip the `np.nan` values with `np.nanmean`, and return the result as a NumPy array. ```python a = np.array([[1.0, 2.0, np.nan], [3.0, np.nan, 6.0], [5.0, 4.0, np.nan]]) column_means_ignore_nan(a) # array([3., 3., 6.]) # column 0: (1 + 3 + 5) / 3, column 1: (2 + 4) / 2, column 2: 6 / 1 ``` ```hint Count what goes into each mean For each column, ask which values belong in the sum and which belong in the count. Then check what your function does for a column where no value is present. ``` ### What a Strong Answer Covers - A reduction along `axis=0` that returns one mean per column as a float array - Why `np.mean` gives the wrong result here and how `np.nanmean` changes both the sum and the count - What happens for a column that is entirely NaN, including the warning NumPy raises - How to get the same means with a mask when `np.nanmean` is not available - Behavior on unusual input such as infinite values or an array with zero rows ### Follow-up Questions - What does `np.nanmean` return for an all-NaN column, and how would you silence the warning or replace that value? - How would you compute the same means without `np.nanmean`? - How would you subtract each column's mean from `a` to center the data, and what changes if you need row means instead? - How do `np.inf` values affect the result, and how would you exclude them too?

Overview: Compute the mean of each column of a 2-D NumPy array while ignoring NaN entries. It tests axis-based reductions, how np.nanmean differs from np.mean, and how to handle columns that are entirely missing without a misleading result or an unexpected warning.

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

Given a 2-D np.ndarray of floats that contains some np.nan entries, write column_means_ignore_nan(a) that returns a 1-D array with the mean of each column, ignoring the missing values.

Reduce along axis=0, so the result has one entry per column. Skip the np.nan values with np.nanmean, and return the result as a NumPy array.

a = np.array([[1.0, 2.0, np.nan],
              [3.0, np.nan, 6.0],
              [5.0, 4.0, np.nan]])

column_means_ignore_nan(a)
# array([3., 3., 6.])
# column 0: (1 + 3 + 5) / 3, column 1: (2 + 4) / 2, column 2: 6 / 1

What a Strong Answer Covers Guidance

  • A reduction along axis=0 that returns one mean per column as a float array
  • Why np.mean gives the wrong result here and how np.nanmean changes both the sum and the count
  • What happens for a column that is entirely NaN, including the warning NumPy raises
  • How to get the same means with a mask when np.nanmean is not available
  • Behavior on unusual input such as infinite values or an array with zero rows

Follow-up Questions Guidance

  • What does np.nanmean return for an all-NaN column, and how would you silence the warning or replace that value?
  • How would you compute the same means without np.nanmean ?
  • How would you subtract each column's mean from a to center the data, and what changes if you need row means instead?
  • How do np.inf values affect the result, and how would you exclude them too?