numpy.nanprod() in Python Last Updated : 01 Jul, 2025 Comments Improve Suggest changes Like Article Like Report numpy.nanprod() function computes the product of array elements over a given axis while treating NaN (Not a Number) values as 1 (i.e., ignoring them in the product). Example: Python import numpy as np a = np.array([1.0, 2.0, np.nan, 4.0]) res = np.nanprod(a) print(res) Output8.0 Explanation: np.nanprod() ignores the NaN and returns the product of 1.0 * 2.0 * 4.0 = 8.0.Syntaxnumpy.nanprod(a, axis=None, dtype=None, keepdims=<no value>)Parameters:a: Input array to compute the product over.axis: Axis or axes along which the product 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 method returns the product of array elements (ignoring NaNs), optionally over the specified axis.ExamplesExample 1: Applying along rows using axis=1 Python import numpy as np a = np.array([[1, 2], [np.nan, 3]]) res = np.nanprod(a, axis=1) print(res) Output[2. 3.] Explanation:Row 0: 1 * 2 = 2Row 1: np.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.nanprod(a, axis=1, keepdims=True) print(res) Output[[2.] [3.]] Explanation: The result keeps the original shape along the reduced axis (columns become 1-column).Example 3: Specifying data type with dtype Python import numpy as np a = np.array([1, 2, 3], dtype=np.int32) res = np.nanprod(a, dtype=np.float64) print(res) Output6.0 Explanation: Converts computation to float64 and computes 1 * 2 * 3 = 6. Comment More infoAdvertise with us Next Article numpy.nanprod() in Python J jana_sayantan Follow Improve Article Tags : Python Python-numpy Python numpy-Mathematical Function Practice Tags : python Similar Reads 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.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 numpy.nonzero() in Python numpy.nonzero() function returns the indices of the elements in an array that are non-zero. It is commonly used to find the positions of non-zero (or True) elements in arrays.Example:Pythonimport numpy as np a = np.array([0, 2, 0, 3, 0, 4]) res = np.nonzero(a) print(res)Output(array([1, 3, 5]),) Exp 2 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.nansum() in Python 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:Pythonimport numpy as np a = np.array([1.0, 2.0, np.nan, 4.0]) res = np.nansum 2 min read Like