NumPy
numpy.org › doc › stable › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v2.5 Manual
>>> import numpy as np >>> np.nan_to_num(np.inf) 1.7976931348623157e+308 >>> np.nan_to_num(-np.inf) -1.7976931348623157e+308 >>> np.nan_to_num(np.nan) 0.0 >>> x = np.array([np.inf, -np.inf, np.nan, -128, 128]) >>> np.nan_to_num(x) array([ 1.79769313e+308, -1.79769313e+308, 0.00000000e+000, # may vary -1.28000000e+002, 1.28000000e+002]) >>> np.nan_to_num(x, nan=-9999, posinf=33333333, neginf=33333333) array([ 3.3333333e+07, 3.3333333e+07, -9.9990000e+03, -1.2800000e+02, 1.2800000e+02]) >>> nan = np.array([11, 12, -9999, 13, 14]) >>> posinf = np.array([33333333, 11, 12, 13, 14]) >>> neginf = np.
Mathematical functions
sin(x, /[, out, where, casting, order, ...]) · Trigonometric sine, element-wise
Linear algebra
Those libraries may be provided by NumPy itself using C versions of a subset of their reference implementations but, when possible, highly optimized libraries that take advantage of specialized processor functionality are preferred. Examples of such libraries are OpenBLAS, MKL (TM), and ATLAS. Because those libraries are multithreaded and processor dependent, environmental variables and external packages such as threadpoolctl may be needed to ...
Array creation routines
empty(shape[, dtype, order, device, like]) · Return a new array of given shape and type, without initializing entries
Array manipulation routines
copyto(dst, src[, casting, where]) · Copies values from one array to another, broadcasting as necessary
NumPy
numpy.org › devdocs › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v2.6.dev0 Manual
>>> import numpy as np >>> np.nan_to_num(np.inf) 1.7976931348623157e+308 >>> np.nan_to_num(-np.inf) -1.7976931348623157e+308 >>> np.nan_to_num(np.nan) 0.0 >>> x = np.array([np.inf, -np.inf, np.nan, -128, 128]) >>> np.nan_to_num(x) array([ 1.79769313e+308, -1.79769313e+308, 0.00000000e+000, # may vary -1.28000000e+002, 1.28000000e+002]) >>> np.nan_to_num(x, nan=-9999, posinf=33333333, neginf=33333333) array([ 3.3333333e+07, 3.3333333e+07, -9.9990000e+03, -1.2800000e+02, 1.2800000e+02]) >>> nan = np.array([11, 12, -9999, 13, 14]) >>> posinf = np.array([33333333, 11, 12, 13, 14]) >>> neginf = np.
01:17
Python NumPy For Your Grandma - 3.5 nan - YouTube
03:07
Handling Missing Values with Numpy - YouTube
06:33
4 np.nan and np.inf - Numpy Crash Course for Data Science | Numpy ...
07:48
How to Create a NumPy Array and Fill It With NaN Values? - YouTube
08:59
[PYTHON][numpy-30] np.nan, np.inf에 대한 이해 - YouTube
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v2.1 Manual
>>> import numpy as np >>> np.nan_to_num(np.inf) 1.7976931348623157e+308 >>> np.nan_to_num(-np.inf) -1.7976931348623157e+308 >>> np.nan_to_num(np.nan) 0.0 >>> x = np.array([np.inf, -np.inf, np.nan, -128, 128]) >>> np.nan_to_num(x) array([ 1.79769313e+308, -1.79769313e+308, 0.00000000e+000, # may vary -1.28000000e+002, 1.28000000e+002]) >>> np.nan_to_num(x, nan=-9999, posinf=33333333, neginf=33333333) array([ 3.3333333e+07, 3.3333333e+07, -9.9990000e+03, -1.2800000e+02, 1.2800000e+02]) >>> y = np.array([complex(np.inf, np.nan), np.nan, complex(np.nan, np.inf)]) array([ 1.79769313e+308, -1.79769313e+308, 0.00000000e+000, # may vary -1.28000000e+002, 1.28000000e+002]) >>> np.nan_to_num(y) array([ 1.79769313e+308 +0.00000000e+000j, # may vary 0.00000000e+000 +0.00000000e+000j, 0.00000000e+000 +1.79769313e+308j]) >>> np.nan_to_num(y, nan=111111, posinf=222222) array([222222.+111111.j, 111111.
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v2.2 Manual
>>> import numpy as np >>> np.nan_to_num(np.inf) 1.7976931348623157e+308 >>> np.nan_to_num(-np.inf) -1.7976931348623157e+308 >>> np.nan_to_num(np.nan) 0.0 >>> x = np.array([np.inf, -np.inf, np.nan, -128, 128]) >>> np.nan_to_num(x) array([ 1.79769313e+308, -1.79769313e+308, 0.00000000e+000, # may vary -1.28000000e+002, 1.28000000e+002]) >>> np.nan_to_num(x, nan=-9999, posinf=33333333, neginf=33333333) array([ 3.3333333e+07, 3.3333333e+07, -9.9990000e+03, -1.2800000e+02, 1.2800000e+02]) >>> y = np.array([complex(np.inf, np.nan), np.nan, complex(np.nan, np.inf)]) array([ 1.79769313e+308, -1.79769313e+308, 0.00000000e+000, # may vary -1.28000000e+002, 1.28000000e+002]) >>> np.nan_to_num(y) array([ 1.79769313e+308 +0.00000000e+000j, # may vary 0.00000000e+000 +0.00000000e+000j, 0.00000000e+000 +1.79769313e+308j]) >>> np.nan_to_num(y, nan=111111, posinf=222222) array([222222.+111111.j, 111111.
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v2.0 Manual
Replace NaN with zero and infinity with large finite numbers (default behaviour) or with the numbers defined by the user using the nan, posinf and/or neginf keywords.
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v2.3 Manual
>>> import numpy as np >>> np.nan_to_num(np.inf) 1.7976931348623157e+308 >>> np.nan_to_num(-np.inf) -1.7976931348623157e+308 >>> np.nan_to_num(np.nan) 0.0 >>> x = np.array([np.inf, -np.inf, np.nan, -128, 128]) >>> np.nan_to_num(x) array([ 1.79769313e+308, -1.79769313e+308, 0.00000000e+000, # may vary -1.28000000e+002, 1.28000000e+002]) >>> np.nan_to_num(x, nan=-9999, posinf=33333333, neginf=33333333) array([ 3.3333333e+07, 3.3333333e+07, -9.9990000e+03, -1.2800000e+02, 1.2800000e+02]) >>> y = np.array([complex(np.inf, np.nan), np.nan, complex(np.nan, np.inf)]) array([ 1.79769313e+308, -1.79769313e+308, 0.00000000e+000, # may vary -1.28000000e+002, 1.28000000e+002]) >>> np.nan_to_num(y) array([ 1.79769313e+308 +0.00000000e+000j, # may vary 0.00000000e+000 +0.00000000e+000j, 0.00000000e+000 +1.79769313e+308j]) >>> np.nan_to_num(y, nan=111111, posinf=222222) array([222222.+111111.j, 111111.
SciPy
docs.scipy.org › doc › numpy-1.13.0 › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v1.13 Manual
June 10, 2017 - Returns an array or scalar replacing Not a Number (NaN) with zero, (positive) infinity with a very large number and negative infinity with a very small (or negative) number.
Top answer 1 of 2
2
numpy.nan_to_num works fine on scalars.
>>> import numpy as np
>>> np.nan_to_num(float('inf'))
1.7976931348623157e+308
>>> np.nan_to_num(float('nan'))
0.0
>>> np.nan_to_num(float('-inf'))
-1.7976931348623157e+308
2 of 2
0
You can just check if your variable of choice is NaN with math.isnan. If it is, change it to the number of your choice. Like this:
>>> import math
>>> x = float("nan")
>>> x
nan
>>> if math.isnan(x):
... x = 123 # just an example
...
>>> x
123
NumPy
numpy.org › doc › 2.4 › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v2.4 Manual
>>> import numpy as np >>> np.nan_to_num(np.inf) 1.7976931348623157e+308 >>> np.nan_to_num(-np.inf) -1.7976931348623157e+308 >>> np.nan_to_num(np.nan) 0.0 >>> x = np.array([np.inf, -np.inf, np.nan, -128, 128]) >>> np.nan_to_num(x) array([ 1.79769313e+308, -1.79769313e+308, 0.00000000e+000, # may vary -1.28000000e+002, 1.28000000e+002]) >>> np.nan_to_num(x, nan=-9999, posinf=33333333, neginf=33333333) array([ 3.3333333e+07, 3.3333333e+07, -9.9990000e+03, -1.2800000e+02, 1.2800000e+02]) >>> nan = np.array([11, 12, -9999, 13, 14]) >>> posinf = np.array([33333333, 11, 12, 13, 14]) >>> neginf = np.
SciPy
docs.scipy.org › doc › numpy-1.10.1 › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v1.10 Manual
Returns an array or scalar replacing Not a Number (NaN) with zero, (positive) infinity with a very large number and negative infinity with a very small (or negative) number.
AskPython
askpython.com › python-modules › numpy › replacing-nan-infinity-python
Usage of nan_to_num in replacing NaN and Infinity - AskPython
February 23, 2023 - The function nan_to_num can eradicate these values making your project error-free. This function is available in the NumPy library and can also be used for replacing infinity with some number which we are going to see in the coming examples. NumPy(Numerical Python) is a library for the Python programming language, which provides large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.
NumPy
numpy.org › doc › 1.16 › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v1.16 Manual
February 18, 2020 - Replace NaN with zero and infinity with large finite numbers.
GeeksforGeeks
geeksforgeeks.org › numpy-nan_to_num-in-python
numpy.nan_to_num() in Python - GeeksforGeeks
November 28, 2018 - numpy.nan_to_num() in Python · ... · Like · Report · numpy.nan_to_num() function is used when we want to replace nan(Not A Number) with zero and inf with finite numbers in an array....
NumPy
numpy.org › doc › 1.14 › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v1.14 Manual
June 12, 2018 - This means that Not a Number is not equivalent to infinity. ... >>> x = np.array([np.inf, -np.inf, np.nan, -128, 128]) >>> np.nan_to_num(x) array([ 1.79769313e+308, -1.79769313e+308, 0.00000000e+000, -1.28000000e+002, 1.28000000e+002]) >>> y = np.array([complex(np.inf, np.nan), np.nan, complex(np.nan, np.inf)]) >>> np.nan_to_num(y) array([ 1.79769313e+308 +0.00000000e+000j, 0.00000000e+000 +0.00000000e+000j, 0.00000000e+000 +1.79769313e+308j])
GeeksforGeeks
geeksforgeeks.org › python › python-numpy-replace-nan-with-zero-and-fill-positive-infinity-for-complex-input-values
Python NumPy - Replace NaN with zero and fill positive infinity for complex input values - GeeksforGeeks
July 23, 2025 - In this article, we will see how to replace NaN with zero and fill positive infinity for complex input values in Python. Numpy package provides us with the numpy.nan_to_num() method to replace NaN with zero and fill positive infinity for complex ...