numpy.nan_to_num() in Python Last Updated : 10 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: Python import numpy as np a = np.array([1.0, np.nan, np.inf, -np.inf]) res = np.nan_to_num(a) print(res) Output[ 1.00000000e+000 0.00000000e+000 1.79769313e+308 -1.79769313e+308] Explanation: By default, NaN is replaced with 0. Infinity values remain unchanged unless specified.Syntaxnumpy.nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None)Parameters:x: Input array (can be scalar, array-like).copy: If True (default), returns a copy. If False, modifies the array in-place.nan: Value to replace NaNs with (default is 0.0).posinf: Value to replace positive infinity with (optional).neginf: Value to replace negative infinity with (optional).Returns: This method returns an array with the same shape as x, where NaN, +inf and -inf are replaced with finite numbers.ExamplesExample 1: Replace only NaNs with zero Python import numpy as np a = np.array([np.nan, 2, 3]) res = np.nan_to_num(a) print(res) Output[0. 2. 3.] Explanation: NaN is replaced with 0, other values are unchanged.Example 2: Replace NaN and infinite values Python import numpy as np a = np.array([np.nan, np.inf, -np.inf, 5]) res = np.nan_to_num(a, nan=0, posinf=1000, neginf=-1000) print(res) Output[ 0. 1000. -1000. 5.] Explanation: NaN → 0, +inf → 1000, -inf → -1000 and regular values like 5 remain unchanged.Example 3: In-place modification with copy=False Python import numpy as np a = np.array([np.nan, np.inf]) np.nan_to_num(a, nan=-1, posinf=9999, copy=False) print(a) Output[-1.000e+00 9.999e+03] Explanation: The original array 'a' is modified directly without making a copy.Related articles: Numpy Comment More infoAdvertise with us Next Article numpy.isnan() in Python J jana_sayantan Follow Improve Article Tags : Python Python-numpy Python numpy-Mathematical Function Practice Tags : python Similar Reads 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 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.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.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.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 Like