numpy.nansum() in Python Last Updated : 30 Jun, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report numpy.nansum() function computes the sum of array elements over a given axis, treating NaN (Not a Number) values as zero. This is useful when you want to ignore missing or undefined values in your computation. For Example: Python import numpy as np a = np.array([1.0, 2.0, np.nan, 4.0]) res = np.nansum(a) print(res) Output7.0 Explanation: np.nansum() ignores the NaN and returns the sum of 1.0 + 2.0 + 4.0 = 7.0.Syntaxnumpy.nansum(a, axis=None, dtype=None, keepdims=<no value>)Parameters:a: Input array to compute the sum over.axis: Axis or axes along which the sum is computed; default is all elements.dtype: Desired data type of the returned array.keepdims: If True, retains reduced dimensions with size one.Returns: This function returns the sum of array elements (ignoring NaNs), optionally over the specified axis.Examples of numpy.nansum() Example 1: Applying along rows using axis=1 Python import numpy as np a = np.array([[1, 2], [np.nan, 3]]) res = np.nansum(a, axis=1) print(res) Output[3. 3.] Explanation:Row 0: 1 + 2 = 3Row 1: NaN is ignored, so 3 is returnedExample 2: Using keepdims=True Python import numpy as np a = np.array([[1, 2], [np.nan, 3]]) res = np.nansum(a, axis=1, keepdims=True) print(res) Output[[3.] [3.]] Explanation: The result keeps the original shape along the reduced axis (1-column instead of reducing to 1D array).Example 3: Specifying data type with dtype Python import numpy as np a = np.array([1, 2, 3], dtype=np.int32) res = np.nansum(a, dtype=np.float64) print(res) Output6.0 Explanation: Computation is done in float64 and the result is the sum 1 + 2 + 3 = 6. Comment More infoAdvertise with us Next Article numpy.nan_to_num() in Python J jana_sayantan Follow Improve Article Tags : Python Python-numpy Python numpy-Mathematical Function Practice Tags : python Similar Reads numpy.nancumsum() in Python numpy.nancumsum() function is used when we want to compute the cumulative sum of array elements over a given axis treating Not a Numbers (NaNs) as zero. The cumulative sum does not change when NaNs are encountered and leading NaNs are replaced by zeros. Zeros are returned for slices that are all-NaN 3 min read numpy.nanmin() in Python numpy.nanmin()function is used when to returns minimum value of an array or along any specific mentioned axis of the array, ignoring any Nan value. Syntax : numpy.nanmin(arr, axis=None, out=None) Parameters : arr :Input array. axis :Axis along which we want the min value. Otherwise, it will consider 2 min read numpy.nan_to_num() in Python numpy.nan_to_num() function replaces NaN (Not a Number) with a specified numerical value (default is 0), and optionally converts positive or negative infinity to finite numbers. Example:Pythonimport numpy as np a = np.array([1.0, np.nan, np.inf, -np.inf]) res = np.nan_to_num(a) print(res)Output[ 1.0 2 min read numpy.nancumprod() in Python numpy.nancumprod() function is used when we want to compute the cumulative product of array elements over a given axis treating Not a Numbers (NaNs) as one. The cumulative product does not change when NaNs are encountered and leading NaNs are replaced by ones. Ones are returned for slices that are a 3 min read numpy.isnan() in Python The numpy.isnan() function tests element-wise whether it is NaN or not and returns the result as a boolean array. Syntax :Â numpy.isnan(array [, out]) Parameters :Â array : [array_like]Input array or object whose elements, we need to test for infinity out : [ndarray, optional]Output array placed wit 2 min read numpy.nanvar() in Python numpy.nanvar(arr, axis = None) : Compute the variance of the given data (array elements) along the specified axis(if any), while ignoring NaN values. Example : x = 1 1 1 1 1 Standard Deviation = 0 . Variance = 0 y = 9, 2, 5, 4, 12, 7, 8, 11, 9, 3, 7, 4, 12, 5, 4, 10, 9, 6, 9, 4 Step 1 : Mean of dist 3 min read Like