Missing markers inside numeric arrays change how NumPy calculations behave because NaN propagates through ordinary arithmetic and many summary results. Filtering those markers is useful when later code should receive only observed values instead of placeholders for missing readings.
np.isnan() checks each value and returns a boolean array that is True only where a NaN appears. Inverting that mask with ~ selects the positions that are not NaN, and boolean indexing returns the matching values.
Element-level masks return the selected values as a one-dimensional array. For table-shaped data, build one boolean value per row with any(axis=1) so complete records stay together while rows containing NaN are removed.
Related: Replace NaN and infinity
Related: Filter with a boolean mask
Related: Calculate statistics
Steps to filter NaN values from a NumPy array:
- Create a script that filters both individual NaN values and rows that contain NaN values.
- nan-filter.py
import numpy as np readings = np.array([21.5, np.nan, 19.0, np.nan, 22.0]) valid_values = ~np.isnan(readings) filtered_values = readings[valid_values] samples = np.array( [ [21.5, 0.98], [np.nan, 0.95], [19.0, np.nan], [22.0, 0.96], ] ) complete_rows = ~np.isnan(samples).any(axis=1) filtered_rows = samples[complete_rows] print("valid value mask:", valid_values) print("filtered values:", filtered_values) print("remaining value NaNs:", np.isnan(filtered_values).any()) print("complete row mask:", complete_rows) print("filtered rows:") print(filtered_rows) print("remaining row NaNs:", np.isnan(filtered_rows).any())
np.isnan() marks only NaN values. Use np.isfinite() when positive and negative infinity should be removed too.
- Run the script and confirm that both filtered results report no remaining NaN values.
$ python nan-filter.py valid value mask: [ True False True False True] filtered values: [21.5 19. 22. ] remaining value NaNs: False complete row mask: [ True False False True] filtered rows: [[21.5 0.98] [22. 0.96]] remaining row NaNs: False
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.