np.count_nonzero(~np.isnan(data))

~ inverts the boolean matrix returned from np.isnan.

np.count_nonzero counts values that is not 0\false. .sum should give the same result. But maybe more clearly to use count_nonzero

Testing speed:

In [23]: data = np.random.random((10000,10000))

In [24]: data[[np.random.random_integers(0,10000, 100)],:][:, [np.random.random_integers(0,99, 100)]] = np.nan

In [25]: %timeit data.size - np.count_nonzero(np.isnan(data))
1 loops, best of 3: 309 ms per loop

In [26]: %timeit np.count_nonzero(~np.isnan(data))
1 loops, best of 3: 345 ms per loop

In [27]: %timeit data.size - np.isnan(data).sum()
1 loops, best of 3: 339 ms per loop

data.size - np.count_nonzero(np.isnan(data)) seems to barely be the fastest here. other data might give different relative speed results.

Answer from M4rtini on Stack Overflow
🌐
Statology
statology.org › home › numpy: how to count number of elements equal to nan
NumPy: How to Count Number of Elements Equal to NaN
February 27, 2023 - The following code shows how to use the count_nonzero() function to count the number of elements in a NumPy array equal to NaN:
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: Count values in an array with conditions | note.nkmk.me
February 3, 2024 - This article explains how to count values in a NumPy array (ndarray) that meet certain conditions. Contents · Count values in an array with a condition: np.count_nonzero() Count values row-wise or column-wise · Check if at least one value meets the condition: np.any() Check if all values meet the condition: np.all() Multiple conditions · Count NaN and non-NaN values ·
🌐
GeeksforGeeks
geeksforgeeks.org › python › counting-the-number-of-non-nan-elements-in-a-numpy-array
Counting the number of non-NaN elements in a NumPy Array - GeeksforGeeks
July 23, 2025 - Default is None, meaning that non-zeros will be counted along a flattened version of arr. Return : [int or array of int] Number of non-zero values in the array along a given axis.
🌐
Kasim Te
kasimte.com › 2020 › 02 › 13 › how-do-i-count-numpy-nans.html
How do I count numpy nans? | Kasim Te
February 13, 2020 - After we have some random indicies, populating the data with np.nan is as simple as setting it. ... In order to count the number of nan instances in the dataset, we can call np.isnan to return a mask of true / false depending on whether the data is nan.
🌐
Arab Psychology
scales.arabpsychology.com › home › how to easily count nan values in a numpy array
How To Easily Count NaN Values In A NumPy Array
November 20, 2025 - np.count_nonzero(): Counts the number of True elements in a boolean array, used here to quantify NaN occurrences. Tilde Operator (~): Used for logical negation, allowing us to invert the boolean mask to count valid (non-NaN) entries. These vectorized operations ensure that counting missing ...
🌐
Plain English
python.plainenglish.io › handling-missing-data-and-nan-values-in-numpy-fa6421182c80
Handling Missing Data and NaN Values in NumPy | by NIBEDITA (NS) | Python in Plain English
August 23, 2026 - In NumPy, we have a built-in function “np.isnan()” to check for NaN values in an array. This will return a Boolean array, means False if the value is a number, and True if not. arr = np.array([1, 2, np.nan, 4, 5]) mask = np.isnan(arr) ...
🌐
GeeksforGeeks
geeksforgeeks.org › counting-the-number-of-non-nan-elements-in-a-numpy-array
Counting the number of non-NaN elements in a NumPy Array - GeeksforGeeks
April 3, 2023 - import numpy as np ex1 = np.array([1, 4, -9, np.nan]) ex2 = np.array([1, 45, -2, np.nan, 3, -np.nan, 3, np.nan]) def approach_1(data): # here the input data, is a numpy ndarray # initialize the number of non-NaN elements # in data count = 0 # loop over each entry of the data for entry in data: # check whether the entry is a non-NaN value # or not if not np.isnan(entry): # if not NaN, increment "count" by 1 count += 1 return count print(approach_1(ex1)) print(approach_1(ex2))
🌐
Reintech
reintech.io › blog › handling-missing-data-nan-values-numpy
Handling Missing Data and NaN Values in NumPy - Reintech
June 13, 2026 - import numpy as np # Sample dataset with missing values temperatures = np.array([22.5, 23.1, np.nan, 24.3, np.nan, 23.8]) # Create boolean mask of NaN locations nan_mask = np.isnan(temperatures) print(f"NaN locations: {nan_mask}") # Output: [False False True False True False] # Count missing values missing_count = np.sum(nan_mask) print(f"Missing values: {missing_count}") # Output: Missing values: 2
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › numpy › numpy_identifying_missing_values.htm
NumPy - Identifying Missing Values
In the following example, we generate a boolean mask using np.isnan() function to identify NaN values in the array. We then count the number of NaN values by summing the mask, which provides the total count of missing values − · import numpy ...
🌐
Medium
medium.com › @amit25173 › understanding-numpy-isnan-what-it-does-1b39c18bb159
Understanding numpy.isnan: What It Does? | by Amit Yadav | Medium
March 6, 2025 - Whether you’re cleaning messy data or counting missing values, this function is a go-to tool in any programmer’s toolkit. ... Imagine you’re working on a dataset, and missing values are messing with your calculations. What can you do? Replace them! Here’s how: import numpy as np # Example array with NaN values arr = np.array([1, 2, np.nan, 4, np.nan, 6]) # Replace NaN values with a specific number, e.g., 0 arr_cleaned = np.where(np.isnan(arr), 0, arr) print("Array after replacing NaN values:", arr_cleaned)
🌐
Codemia
codemia.io › home › knowledge hub › how do i count the occurrence of a certain item in an ndarray?
How do I count the occurrence of a certain item in an ndarray? | Codemia
September 23, 2025 - That means direct comparison will never count missing values correctly. ... 1import numpy as np 2 3arr = np.array([1.0, np.nan, np.nan, 2.0]) 4count_nan = np.count_nonzero(np.isnan(arr)) 5 6print(count_nan)
🌐
Medium
medium.com › @amit25173 › numpy-isnan-in-python-63b8c185331e
numpy.isnan in Python. If you think you need to spend $2,000… | by Amit Yadav | Medium
February 9, 2025 - Whether you’re cleaning messy data or counting missing values, this function is a go-to tool in any programmer’s toolkit. ... Imagine you’re working on a dataset, and missing values are messing with your calculations. What can you do? Replace them! Here’s how: import numpy as np # Example array with NaN values arr = np.array([1, 2, np.nan, 4, np.nan, 6]) # Replace NaN values with a specific number, e.g., 0 arr_cleaned = np.where(np.isnan(arr), 0, arr) print("Array after replacing NaN values:", arr_cleaned)
🌐
Appdividend
appdividend.com › count-the-number-of-none-and-nan-values-in-python-list
How to Count the Number of None and NaN Values in Python List
September 6, 2025 - To count NaN values in Python, use “generator expression,” where we efficiently iterate a main list, use conditional filtering and boolean summation, and finally, using the sum() function, count the True boolean values, effectively counting ...
🌐
Python Guides
pythonguides.com › python-numpy-count
np.count_nonzero() Function in NumPy
May 16, 2025 - Learn how to count non-zero elements in NumPy arrays using np.count_nonzero(). Includes examples with conditions, multidimensional arrays, and performance tips.
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.isnan.html
numpy.isnan — NumPy v2.5 Manual
>>> import numpy as np >>> np.isnan(np.nan) True >>> np.isnan(np.inf) False >>> np.isnan([np.log(-1.),1.,np.log(0)]) array([ True, False, False])
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.isnan.html
numpy.isnan — NumPy v2.1 Manual
>>> import numpy as np >>> np.isnan(np.nan) True >>> np.isnan(np.inf) False >>> np.isnan([np.log(-1.),1.,np.log(0)]) array([ True, False, False])