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
Answer from senshin on Stack Overflow
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v2.6.dev0 Manual
If no values are passed then NaN values will be replaced with 0.0. posinfint, float, or bool or array_like of int, float, or bool, optional · Values to be used to fill positive infinity values. If no values are passed then positive infinity values will be replaced with a very large number.
Learn
This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference. ... This set of tutorials and educational materials is being developed in the numpy-tutorials repository, and is not a part of the NumPy source tree. The goal of this repository is to provide ...
Release notes
Fundamentals and usage · Advanced usage and interoperability
Constants
Also that positive infinity is not equivalent to negative infinity. But infinity is equivalent to positive infinity. ... Try it in your browser! >>> import numpy as np >>> np.inf inf >>> np.array([1]) / 0. array([inf]) ... IEEE 754 floating point representation of Not a Number (NaN).
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 ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › numpy-nan_to_num-in-python
numpy.nan_to_num() in Python - GeeksforGeeks
July 10, 2025 - 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.
🌐
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.
🌐
Medium
medium.com › @amit25173 › essential-steps-to-mastering-numpy-nan-to-num-conversion-50435ee0661d
Essential Steps to Mastering Numpy NaN to Num Conversion | by Amit Yadav | Medium
April 12, 2025 - This function replaces NaN values with a specified numerical value, which can be quite handy. Here’s how you can use numpy.nan_to_num() in your code:
🌐
Omz Software
omz-software.com › pythonista › numpy › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v1.8 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.
🌐
JAX Documentation
docs.jax.dev › en › latest › _autosummary › jax.numpy.nan_to_num.html
jax.numpy.nan_to_num — JAX documentation
>>> jnp.nan_to_num(x) Array([ 0.0000000e+00, 0.0000000e+00, 1.0000000e+00, 3.4028235e+38, 2.0000000e+00, -3.4028235e+38], dtype=float32) ... If you only wish to substitute for NaN values while leaving inf values untouched, using where() with jax.numpy.isnan() is a better option:
Find elsewhere
🌐
AskPython
askpython.com › python-modules › numpy › replacing-nan-infinity-python
Usage of nan_to_num in replacing NaN and Infinity - AskPython
February 23, 2023 - nan_to_num is a function of numpy library that converts NaN (not a number) to a numeric value. The NaN is generally replaced with zero whenever we call upon
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v2.2 Manual
This means that Not a Number is not equivalent to infinity. ... >>> 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.
🌐
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.
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.nansum.html
numpy.nansum — NumPy v2.1 Manual
Return the sum of array elements over a given axis treating Not a Numbers (NaNs) as zero.
🌐
NumPy
numpy.org › doc › 1.16 › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v1.16 Manual
February 18, 2020 - This means that Not a Number is not equivalent to infinity. ... >>> 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, -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])
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v2.1 Manual
This means that Not a Number is not equivalent to infinity. ... >>> 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.
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v2.3 Manual
This means that Not a Number is not equivalent to infinity. ... Try it in your browser! >>> 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([comp
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.nansum.html
numpy.nansum — NumPy v2.6.dev0 Manual
Return the sum of array elements over a given axis treating Not a Numbers (NaNs) as zero.
🌐
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])