ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 1.7 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://numpy.org/doc/2.2/reference/generated/numpy.nan_to_num.html |
| Last Crawled | 2026-02-26 12:59:00 (1 month ago) |
| First Indexed | 2024-12-15 09:00:48 (1 year ago) |
| HTTP Status Code | 200 |
| Meta Title | numpy.nan_to_num — NumPy v2.2 Manual |
| Meta Description | null |
| Meta Canonical | null |
| Boilerpipe Text | numpy.
nan_to_num
(
x
,
copy
=
True
,
nan
=
0.0
,
posinf
=
None
,
neginf
=
None
)
[source]
#
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.
If
x
is inexact, NaN is replaced by zero or by the user defined value in
nan
keyword, infinity is replaced by the largest finite floating point
values representable by
x.dtype
or by the user defined value in
posinf
keyword and -infinity is replaced by the most negative finite
floating point values representable by
x.dtype
or by the user defined
value in
neginf
keyword.
For complex dtypes, the above is applied to each of the real and
imaginary components of
x
separately.
If
x
is not inexact, then no replacements are made.
Parameters
:
x
scalar or array_like
Input data.
copy
bool, optional
Whether to create a copy of
x
(True) or to replace values
in-place (False). The in-place operation only occurs if
casting to an array does not require a copy.
Default is True.
nan
int, float, optional
Value to be used to fill NaN values. If no value is passed
then NaN values will be replaced with 0.0.
posinf
int, float, optional
Value to be used to fill positive infinity values. If no value is
passed then positive infinity values will be replaced with a very
large number.
neginf
int, float, optional
Value to be used to fill negative infinity values. If no value is
passed then negative infinity values will be replaced with a very
small (or negative) number.
Returns
:
out
ndarray
x
, with the non-finite values replaced. If
copy
is False, this may
be
x
itself.
See also
isinf
Shows which elements are positive or negative infinity.
isneginf
Shows which elements are negative infinity.
isposinf
Shows which elements are positive infinity.
isnan
Shows which elements are Not a Number (NaN).
isfinite
Shows which elements are finite (not NaN, not infinity)
Notes
NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic
(IEEE 754). This means that Not a Number is not equivalent to infinity.
Examples
>>>
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. +0.j, 111111.+222222.j]) |
| Markdown | [Skip to main content](https://numpy.org/doc/2.2/reference/generated/numpy.nan_to_num.html#main-content)
Back to top
This is documentation for **an old version (2.2.0)**.[Switch to stable version](https://numpy.org/doc/stable/reference/generated/numpy.nan_to_num.html)
[ ](https://numpy.org/doc/2.2/index.html)
- [User Guide](https://numpy.org/doc/2.2/user/index.html)
- [API reference](https://numpy.org/doc/2.2/reference/index.html)
- [Building from source](https://numpy.org/doc/2.2/building/index.html)
- [Development](https://numpy.org/doc/2.2/dev/index.html)
- [Release notes](https://numpy.org/doc/2.2/release.html)
- [Learn](https://numpy.org/numpy-tutorials/)
- More
- [NEPs](https://numpy.org/neps)
2\.2
[dev](https://numpy.org/devdocs/reference/generated/numpy.nan_to_num.html)[2\.4 (stable)](https://numpy.org/doc/stable/reference/generated/numpy.nan_to_num.html)[2\.3](https://numpy.org/doc/2.3/reference/generated/numpy.nan_to_num.html)[2\.2](https://numpy.org/doc/2.2/reference/generated/numpy.nan_to_num.html)[2\.1](https://numpy.org/doc/2.1/reference/generated/numpy.nan_to_num.html)[2\.0](https://numpy.org/doc/2.0/reference/generated/numpy.nan_to_num.html)[1\.26](https://numpy.org/doc/1.26/reference/generated/numpy.nan_to_num.html)[1\.25](https://numpy.org/doc/1.25/reference/generated/numpy.nan_to_num.html)[1\.24](https://numpy.org/doc/1.24/reference/generated/numpy.nan_to_num.html)[1\.23](https://numpy.org/doc/1.23/reference/generated/numpy.nan_to_num.html)[1\.22](https://numpy.org/doc/1.22/reference/generated/numpy.nan_to_num.html)[1\.21](https://numpy.org/doc/1.21/reference/generated/numpy.nan_to_num.html)[1\.20](https://numpy.org/doc/1.20/reference/generated/numpy.nan_to_num.html)[1\.19](https://numpy.org/doc/1.19/reference/generated/numpy.nan_to_num.html)[1\.18](https://numpy.org/doc/1.18/reference/generated/numpy.nan_to_num.html)[1\.17](https://numpy.org/doc/1.17/reference/generated/numpy.nan_to_num.html)[1\.16](https://numpy.org/doc/1.16/reference/generated/numpy.nan_to_num.html)[1\.15](https://numpy.org/doc/1.15/reference/generated/numpy.nan_to_num.html)[1\.14](https://numpy.org/doc/1.14/reference/generated/numpy.nan_to_num.html)[1\.13](https://numpy.org/doc/1.13/reference/generated/numpy.nan_to_num.html)
- [GitHub](https://github.com/numpy/numpy)
- [User Guide](https://numpy.org/doc/2.2/user/index.html)
- [API reference](https://numpy.org/doc/2.2/reference/index.html)
- [Building from source](https://numpy.org/doc/2.2/building/index.html)
- [Development](https://numpy.org/doc/2.2/dev/index.html)
- [Release notes](https://numpy.org/doc/2.2/release.html)
- [Learn](https://numpy.org/numpy-tutorials/)
- [NEPs](https://numpy.org/neps)
2\.2
[dev](https://numpy.org/devdocs/reference/generated/numpy.nan_to_num.html)[2\.4 (stable)](https://numpy.org/doc/stable/reference/generated/numpy.nan_to_num.html)[2\.3](https://numpy.org/doc/2.3/reference/generated/numpy.nan_to_num.html)[2\.2](https://numpy.org/doc/2.2/reference/generated/numpy.nan_to_num.html)[2\.1](https://numpy.org/doc/2.1/reference/generated/numpy.nan_to_num.html)[2\.0](https://numpy.org/doc/2.0/reference/generated/numpy.nan_to_num.html)[1\.26](https://numpy.org/doc/1.26/reference/generated/numpy.nan_to_num.html)[1\.25](https://numpy.org/doc/1.25/reference/generated/numpy.nan_to_num.html)[1\.24](https://numpy.org/doc/1.24/reference/generated/numpy.nan_to_num.html)[1\.23](https://numpy.org/doc/1.23/reference/generated/numpy.nan_to_num.html)[1\.22](https://numpy.org/doc/1.22/reference/generated/numpy.nan_to_num.html)[1\.21](https://numpy.org/doc/1.21/reference/generated/numpy.nan_to_num.html)[1\.20](https://numpy.org/doc/1.20/reference/generated/numpy.nan_to_num.html)[1\.19](https://numpy.org/doc/1.19/reference/generated/numpy.nan_to_num.html)[1\.18](https://numpy.org/doc/1.18/reference/generated/numpy.nan_to_num.html)[1\.17](https://numpy.org/doc/1.17/reference/generated/numpy.nan_to_num.html)[1\.16](https://numpy.org/doc/1.16/reference/generated/numpy.nan_to_num.html)[1\.15](https://numpy.org/doc/1.15/reference/generated/numpy.nan_to_num.html)[1\.14](https://numpy.org/doc/1.14/reference/generated/numpy.nan_to_num.html)[1\.13](https://numpy.org/doc/1.13/reference/generated/numpy.nan_to_num.html)
- [GitHub](https://github.com/numpy/numpy)
Section Navigation
- [NumPy’s module structure](https://numpy.org/doc/2.2/reference/module_structure.html)
- [Array objects](https://numpy.org/doc/2.2/reference/arrays.html)
- [Universal functions (`ufunc`)](https://numpy.org/doc/2.2/reference/ufuncs.html)
- [Routines and objects by topic](https://numpy.org/doc/2.2/reference/routines.html)
- [Constants](https://numpy.org/doc/2.2/reference/constants.html)
- [Array creation routines](https://numpy.org/doc/2.2/reference/routines.array-creation.html)
- [Array manipulation routines](https://numpy.org/doc/2.2/reference/routines.array-manipulation.html)
- [Bit-wise operations](https://numpy.org/doc/2.2/reference/routines.bitwise.html)
- [String functionality](https://numpy.org/doc/2.2/reference/routines.strings.html)
- [Datetime support functions](https://numpy.org/doc/2.2/reference/routines.datetime.html)
- [Data type routines](https://numpy.org/doc/2.2/reference/routines.dtype.html)
- [Mathematical functions with automatic domain](https://numpy.org/doc/2.2/reference/routines.emath.html)
- [Floating point error handling](https://numpy.org/doc/2.2/reference/routines.err.html)
- [Exceptions and Warnings (`numpy.exceptions`)](https://numpy.org/doc/2.2/reference/routines.exceptions.html)
- [Discrete Fourier Transform (`numpy.fft`)](https://numpy.org/doc/2.2/reference/routines.fft.html)
- [Functional programming](https://numpy.org/doc/2.2/reference/routines.functional.html)
- [Input and output](https://numpy.org/doc/2.2/reference/routines.io.html)
- [Indexing routines](https://numpy.org/doc/2.2/reference/routines.indexing.html)
- [Linear algebra (`numpy.linalg`)](https://numpy.org/doc/2.2/reference/routines.linalg.html)
- [Logic functions](https://numpy.org/doc/2.2/reference/routines.logic.html)
- [Masked array operations](https://numpy.org/doc/2.2/reference/routines.ma.html)
- [Mathematical functions](https://numpy.org/doc/2.2/reference/routines.math.html)
- [numpy.sin](https://numpy.org/doc/2.2/reference/generated/numpy.sin.html)
- [numpy.cos](https://numpy.org/doc/2.2/reference/generated/numpy.cos.html)
- [numpy.tan](https://numpy.org/doc/2.2/reference/generated/numpy.tan.html)
- [numpy.arcsin](https://numpy.org/doc/2.2/reference/generated/numpy.arcsin.html)
- [numpy.asin](https://numpy.org/doc/2.2/reference/generated/numpy.asin.html)
- [numpy.arccos](https://numpy.org/doc/2.2/reference/generated/numpy.arccos.html)
- [numpy.acos](https://numpy.org/doc/2.2/reference/generated/numpy.acos.html)
- [numpy.arctan](https://numpy.org/doc/2.2/reference/generated/numpy.arctan.html)
- [numpy.atan](https://numpy.org/doc/2.2/reference/generated/numpy.atan.html)
- [numpy.hypot](https://numpy.org/doc/2.2/reference/generated/numpy.hypot.html)
- [numpy.arctan2](https://numpy.org/doc/2.2/reference/generated/numpy.arctan2.html)
- [numpy.atan2](https://numpy.org/doc/2.2/reference/generated/numpy.atan2.html)
- [numpy.degrees](https://numpy.org/doc/2.2/reference/generated/numpy.degrees.html)
- [numpy.radians](https://numpy.org/doc/2.2/reference/generated/numpy.radians.html)
- [numpy.unwrap](https://numpy.org/doc/2.2/reference/generated/numpy.unwrap.html)
- [numpy.deg2rad](https://numpy.org/doc/2.2/reference/generated/numpy.deg2rad.html)
- [numpy.rad2deg](https://numpy.org/doc/2.2/reference/generated/numpy.rad2deg.html)
- [numpy.sinh](https://numpy.org/doc/2.2/reference/generated/numpy.sinh.html)
- [numpy.cosh](https://numpy.org/doc/2.2/reference/generated/numpy.cosh.html)
- [numpy.tanh](https://numpy.org/doc/2.2/reference/generated/numpy.tanh.html)
- [numpy.arcsinh](https://numpy.org/doc/2.2/reference/generated/numpy.arcsinh.html)
- [numpy.asinh](https://numpy.org/doc/2.2/reference/generated/numpy.asinh.html)
- [numpy.arccosh](https://numpy.org/doc/2.2/reference/generated/numpy.arccosh.html)
- [numpy.acosh](https://numpy.org/doc/2.2/reference/generated/numpy.acosh.html)
- [numpy.arctanh](https://numpy.org/doc/2.2/reference/generated/numpy.arctanh.html)
- [numpy.atanh](https://numpy.org/doc/2.2/reference/generated/numpy.atanh.html)
- [numpy.round](https://numpy.org/doc/2.2/reference/generated/numpy.round.html)
- [numpy.around](https://numpy.org/doc/2.2/reference/generated/numpy.around.html)
- [numpy.rint](https://numpy.org/doc/2.2/reference/generated/numpy.rint.html)
- [numpy.fix](https://numpy.org/doc/2.2/reference/generated/numpy.fix.html)
- [numpy.floor](https://numpy.org/doc/2.2/reference/generated/numpy.floor.html)
- [numpy.ceil](https://numpy.org/doc/2.2/reference/generated/numpy.ceil.html)
- [numpy.trunc](https://numpy.org/doc/2.2/reference/generated/numpy.trunc.html)
- [numpy.prod](https://numpy.org/doc/2.2/reference/generated/numpy.prod.html)
- [numpy.sum](https://numpy.org/doc/2.2/reference/generated/numpy.sum.html)
- [numpy.nanprod](https://numpy.org/doc/2.2/reference/generated/numpy.nanprod.html)
- [numpy.nansum](https://numpy.org/doc/2.2/reference/generated/numpy.nansum.html)
- [numpy.cumulative\_sum](https://numpy.org/doc/2.2/reference/generated/numpy.cumulative_sum.html)
- [numpy.cumulative\_prod](https://numpy.org/doc/2.2/reference/generated/numpy.cumulative_prod.html)
- [numpy.cumprod](https://numpy.org/doc/2.2/reference/generated/numpy.cumprod.html)
- [numpy.cumsum](https://numpy.org/doc/2.2/reference/generated/numpy.cumsum.html)
- [numpy.nancumprod](https://numpy.org/doc/2.2/reference/generated/numpy.nancumprod.html)
- [numpy.nancumsum](https://numpy.org/doc/2.2/reference/generated/numpy.nancumsum.html)
- [numpy.diff](https://numpy.org/doc/2.2/reference/generated/numpy.diff.html)
- [numpy.ediff1d](https://numpy.org/doc/2.2/reference/generated/numpy.ediff1d.html)
- [numpy.gradient](https://numpy.org/doc/2.2/reference/generated/numpy.gradient.html)
- [numpy.cross](https://numpy.org/doc/2.2/reference/generated/numpy.cross.html)
- [numpy.trapezoid](https://numpy.org/doc/2.2/reference/generated/numpy.trapezoid.html)
- [numpy.exp](https://numpy.org/doc/2.2/reference/generated/numpy.exp.html)
- [numpy.expm1](https://numpy.org/doc/2.2/reference/generated/numpy.expm1.html)
- [numpy.exp2](https://numpy.org/doc/2.2/reference/generated/numpy.exp2.html)
- [numpy.log](https://numpy.org/doc/2.2/reference/generated/numpy.log.html)
- [numpy.log10](https://numpy.org/doc/2.2/reference/generated/numpy.log10.html)
- [numpy.log2](https://numpy.org/doc/2.2/reference/generated/numpy.log2.html)
- [numpy.log1p](https://numpy.org/doc/2.2/reference/generated/numpy.log1p.html)
- [numpy.logaddexp](https://numpy.org/doc/2.2/reference/generated/numpy.logaddexp.html)
- [numpy.logaddexp2](https://numpy.org/doc/2.2/reference/generated/numpy.logaddexp2.html)
- [numpy.i0](https://numpy.org/doc/2.2/reference/generated/numpy.i0.html)
- [numpy.sinc](https://numpy.org/doc/2.2/reference/generated/numpy.sinc.html)
- [numpy.signbit](https://numpy.org/doc/2.2/reference/generated/numpy.signbit.html)
- [numpy.copysign](https://numpy.org/doc/2.2/reference/generated/numpy.copysign.html)
- [numpy.frexp](https://numpy.org/doc/2.2/reference/generated/numpy.frexp.html)
- [numpy.ldexp](https://numpy.org/doc/2.2/reference/generated/numpy.ldexp.html)
- [numpy.nextafter](https://numpy.org/doc/2.2/reference/generated/numpy.nextafter.html)
- [numpy.spacing](https://numpy.org/doc/2.2/reference/generated/numpy.spacing.html)
- [numpy.lcm](https://numpy.org/doc/2.2/reference/generated/numpy.lcm.html)
- [numpy.gcd](https://numpy.org/doc/2.2/reference/generated/numpy.gcd.html)
- [numpy.add](https://numpy.org/doc/2.2/reference/generated/numpy.add.html)
- [numpy.reciprocal](https://numpy.org/doc/2.2/reference/generated/numpy.reciprocal.html)
- [numpy.positive](https://numpy.org/doc/2.2/reference/generated/numpy.positive.html)
- [numpy.negative](https://numpy.org/doc/2.2/reference/generated/numpy.negative.html)
- [numpy.multiply](https://numpy.org/doc/2.2/reference/generated/numpy.multiply.html)
- [numpy.divide](https://numpy.org/doc/2.2/reference/generated/numpy.divide.html)
- [numpy.power](https://numpy.org/doc/2.2/reference/generated/numpy.power.html)
- [numpy.pow](https://numpy.org/doc/2.2/reference/generated/numpy.pow.html)
- [numpy.subtract](https://numpy.org/doc/2.2/reference/generated/numpy.subtract.html)
- [numpy.true\_divide](https://numpy.org/doc/2.2/reference/generated/numpy.true_divide.html)
- [numpy.floor\_divide](https://numpy.org/doc/2.2/reference/generated/numpy.floor_divide.html)
- [numpy.float\_power](https://numpy.org/doc/2.2/reference/generated/numpy.float_power.html)
- [numpy.fmod](https://numpy.org/doc/2.2/reference/generated/numpy.fmod.html)
- [numpy.mod](https://numpy.org/doc/2.2/reference/generated/numpy.mod.html)
- [numpy.modf](https://numpy.org/doc/2.2/reference/generated/numpy.modf.html)
- [numpy.remainder](https://numpy.org/doc/2.2/reference/generated/numpy.remainder.html)
- [numpy.divmod](https://numpy.org/doc/2.2/reference/generated/numpy.divmod.html)
- [numpy.angle](https://numpy.org/doc/2.2/reference/generated/numpy.angle.html)
- [numpy.real](https://numpy.org/doc/2.2/reference/generated/numpy.real.html)
- [numpy.imag](https://numpy.org/doc/2.2/reference/generated/numpy.imag.html)
- [numpy.conj](https://numpy.org/doc/2.2/reference/generated/numpy.conj.html)
- [numpy.conjugate](https://numpy.org/doc/2.2/reference/generated/numpy.conjugate.html)
- [numpy.maximum](https://numpy.org/doc/2.2/reference/generated/numpy.maximum.html)
- [numpy.max](https://numpy.org/doc/2.2/reference/generated/numpy.max.html)
- [numpy.amax](https://numpy.org/doc/2.2/reference/generated/numpy.amax.html)
- [numpy.fmax](https://numpy.org/doc/2.2/reference/generated/numpy.fmax.html)
- [numpy.nanmax](https://numpy.org/doc/2.2/reference/generated/numpy.nanmax.html)
- [numpy.minimum](https://numpy.org/doc/2.2/reference/generated/numpy.minimum.html)
- [numpy.min](https://numpy.org/doc/2.2/reference/generated/numpy.min.html)
- [numpy.amin](https://numpy.org/doc/2.2/reference/generated/numpy.amin.html)
- [numpy.fmin](https://numpy.org/doc/2.2/reference/generated/numpy.fmin.html)
- [numpy.nanmin](https://numpy.org/doc/2.2/reference/generated/numpy.nanmin.html)
- [numpy.convolve](https://numpy.org/doc/2.2/reference/generated/numpy.convolve.html)
- [numpy.clip](https://numpy.org/doc/2.2/reference/generated/numpy.clip.html)
- [numpy.sqrt](https://numpy.org/doc/2.2/reference/generated/numpy.sqrt.html)
- [numpy.cbrt](https://numpy.org/doc/2.2/reference/generated/numpy.cbrt.html)
- [numpy.square](https://numpy.org/doc/2.2/reference/generated/numpy.square.html)
- [numpy.absolute](https://numpy.org/doc/2.2/reference/generated/numpy.absolute.html)
- [numpy.fabs](https://numpy.org/doc/2.2/reference/generated/numpy.fabs.html)
- [numpy.sign](https://numpy.org/doc/2.2/reference/generated/numpy.sign.html)
- [numpy.heaviside](https://numpy.org/doc/2.2/reference/generated/numpy.heaviside.html)
- [numpy.nan\_to\_num](https://numpy.org/doc/2.2/reference/generated/numpy.nan_to_num.html)
- [numpy.real\_if\_close](https://numpy.org/doc/2.2/reference/generated/numpy.real_if_close.html)
- [numpy.interp](https://numpy.org/doc/2.2/reference/generated/numpy.interp.html)
- [numpy.bitwise\_count](https://numpy.org/doc/2.2/reference/generated/numpy.bitwise_count.html)
- [Miscellaneous routines](https://numpy.org/doc/2.2/reference/routines.other.html)
- [Polynomials](https://numpy.org/doc/2.2/reference/routines.polynomials.html)
- [Random sampling (`numpy.random`)](https://numpy.org/doc/2.2/reference/random/index.html)
- [Set routines](https://numpy.org/doc/2.2/reference/routines.set.html)
- [Sorting, searching, and counting](https://numpy.org/doc/2.2/reference/routines.sort.html)
- [Statistics](https://numpy.org/doc/2.2/reference/routines.statistics.html)
- [Test support (`numpy.testing`)](https://numpy.org/doc/2.2/reference/routines.testing.html)
- [Window functions](https://numpy.org/doc/2.2/reference/routines.window.html)
- [Typing (`numpy.typing`)](https://numpy.org/doc/2.2/reference/typing.html)
- [Packaging (`numpy.distutils`)](https://numpy.org/doc/2.2/reference/distutils.html)
- [NumPy C-API](https://numpy.org/doc/2.2/reference/c-api/index.html)
- [Array API standard compatibility](https://numpy.org/doc/2.2/reference/array_api.html)
- [CPU/SIMD optimizations](https://numpy.org/doc/2.2/reference/simd/index.html)
- [Thread Safety](https://numpy.org/doc/2.2/reference/thread_safety.html)
- [Global Configuration Options](https://numpy.org/doc/2.2/reference/global_state.html)
- [NumPy security](https://numpy.org/doc/2.2/reference/security.html)
- [Status of `numpy.distutils` and migration advice](https://numpy.org/doc/2.2/reference/distutils_status_migration.html)
- [`numpy.distutils` user guide](https://numpy.org/doc/2.2/reference/distutils_guide.html)
- [NumPy and SWIG](https://numpy.org/doc/2.2/reference/swig.html)
- [NumPy reference](https://numpy.org/doc/2.2/reference/index.html)
- [Routines and objects by topic](https://numpy.org/doc/2.2/reference/routines.html)
- [Mathematical functions](https://numpy.org/doc/2.2/reference/routines.math.html)
- numpy.nan\_to\_num
# numpy.nan\_to\_num[\#](https://numpy.org/doc/2.2/reference/generated/numpy.nan_to_num.html#numpy-nan-to-num "Link to this heading")
numpy.nan\_to\_num(*x*, *copy\=True*, *nan\=0\.0*, *posinf\=None*, *neginf\=None*)[\[source\]](https://github.com/numpy/numpy/blob/v2.2.0/numpy/lib/_type_check_impl.py#L373-L481)[\#](https://numpy.org/doc/2.2/reference/generated/numpy.nan_to_num.html#numpy.nan_to_num "Link to this definition")
Replace NaN with zero and infinity with large finite numbers (default behaviour) or with the numbers defined by the user using the [`nan`](https://numpy.org/doc/2.2/reference/constants.html#numpy.nan "numpy.nan"), *posinf* and/or *neginf* keywords.
If *x* is inexact, NaN is replaced by zero or by the user defined value in [`nan`](https://numpy.org/doc/2.2/reference/constants.html#numpy.nan "numpy.nan") keyword, infinity is replaced by the largest finite floating point values representable by `x.dtype` or by the user defined value in *posinf* keyword and -infinity is replaced by the most negative finite floating point values representable by `x.dtype` or by the user defined value in *neginf* keyword.
For complex dtypes, the above is applied to each of the real and imaginary components of *x* separately.
If *x* is not inexact, then no replacements are made.
Parameters:
**x**scalar or array\_like
Input data.
**copy**bool, optional
Whether to create a copy of *x* (True) or to replace values in-place (False). The in-place operation only occurs if casting to an array does not require a copy. Default is True.
**nan**int, float, optional
Value to be used to fill NaN values. If no value is passed then NaN values will be replaced with 0.0.
**posinf**int, float, optional
Value to be used to fill positive infinity values. If no value is passed then positive infinity values will be replaced with a very large number.
**neginf**int, float, optional
Value to be used to fill negative infinity values. If no value is passed then negative infinity values will be replaced with a very small (or negative) number.
Returns:
**out**ndarray
*x*, with the non-finite values replaced. If [`copy`](https://numpy.org/doc/2.2/reference/generated/numpy.copy.html#numpy.copy "numpy.copy") is False, this may be *x* itself.
See also
[`isinf`](https://numpy.org/doc/2.2/reference/generated/numpy.isinf.html#numpy.isinf "numpy.isinf")
Shows which elements are positive or negative infinity.
[`isneginf`](https://numpy.org/doc/2.2/reference/generated/numpy.isneginf.html#numpy.isneginf "numpy.isneginf")
Shows which elements are negative infinity.
[`isposinf`](https://numpy.org/doc/2.2/reference/generated/numpy.isposinf.html#numpy.isposinf "numpy.isposinf")
Shows which elements are positive infinity.
[`isnan`](https://numpy.org/doc/2.2/reference/generated/numpy.isnan.html#numpy.isnan "numpy.isnan")
Shows which elements are Not a Number (NaN).
[`isfinite`](https://numpy.org/doc/2.2/reference/generated/numpy.isfinite.html#numpy.isfinite "numpy.isfinite")
Shows which elements are finite (not NaN, not infinity)
Notes
NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic (IEEE 754). This means that Not a Number is not equivalent to infinity.
Examples
```
>>> 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. +0.j, 111111.+222222.j])
```
[previous numpy.heaviside](https://numpy.org/doc/2.2/reference/generated/numpy.heaviside.html "previous page")
[next numpy.real\_if\_close](https://numpy.org/doc/2.2/reference/generated/numpy.real_if_close.html "next page")
On this page
- [`nan_to_num`](https://numpy.org/doc/2.2/reference/generated/numpy.nan_to_num.html#numpy.nan_to_num)
© Copyright 2008-2024, NumPy Developers.
Created using [Sphinx](https://www.sphinx-doc.org/) 7.2.6.
Built with the [PyData Sphinx Theme](https://pydata-sphinx-theme.readthedocs.io/en/stable/index.html) 0.16.0. |
| Readable Markdown | numpy.nan\_to\_num(*x*, *copy\=True*, *nan\=0\.0*, *posinf\=None*, *neginf\=None*)[\[source\]](https://github.com/numpy/numpy/blob/v2.2.0/numpy/lib/_type_check_impl.py#L373-L481)[\#](https://numpy.org/doc/2.2/reference/generated/numpy.nan_to_num.html#numpy.nan_to_num "Link to this definition")
Replace NaN with zero and infinity with large finite numbers (default behaviour) or with the numbers defined by the user using the [`nan`](https://numpy.org/doc/2.2/reference/constants.html#numpy.nan "numpy.nan"), *posinf* and/or *neginf* keywords.
If *x* is inexact, NaN is replaced by zero or by the user defined value in [`nan`](https://numpy.org/doc/2.2/reference/constants.html#numpy.nan "numpy.nan") keyword, infinity is replaced by the largest finite floating point values representable by `x.dtype` or by the user defined value in *posinf* keyword and -infinity is replaced by the most negative finite floating point values representable by `x.dtype` or by the user defined value in *neginf* keyword.
For complex dtypes, the above is applied to each of the real and imaginary components of *x* separately.
If *x* is not inexact, then no replacements are made.
Parameters:
**x**scalar or array\_like
Input data.
**copy**bool, optional
Whether to create a copy of *x* (True) or to replace values in-place (False). The in-place operation only occurs if casting to an array does not require a copy. Default is True.
**nan**int, float, optional
Value to be used to fill NaN values. If no value is passed then NaN values will be replaced with 0.0.
**posinf**int, float, optional
Value to be used to fill positive infinity values. If no value is passed then positive infinity values will be replaced with a very large number.
**neginf**int, float, optional
Value to be used to fill negative infinity values. If no value is passed then negative infinity values will be replaced with a very small (or negative) number.
Returns:
**out**ndarray
*x*, with the non-finite values replaced. If [`copy`](https://numpy.org/doc/2.2/reference/generated/numpy.copy.html#numpy.copy "numpy.copy") is False, this may be *x* itself.
See also
[`isinf`](https://numpy.org/doc/2.2/reference/generated/numpy.isinf.html#numpy.isinf "numpy.isinf")
Shows which elements are positive or negative infinity.
[`isneginf`](https://numpy.org/doc/2.2/reference/generated/numpy.isneginf.html#numpy.isneginf "numpy.isneginf")
Shows which elements are negative infinity.
[`isposinf`](https://numpy.org/doc/2.2/reference/generated/numpy.isposinf.html#numpy.isposinf "numpy.isposinf")
Shows which elements are positive infinity.
[`isnan`](https://numpy.org/doc/2.2/reference/generated/numpy.isnan.html#numpy.isnan "numpy.isnan")
Shows which elements are Not a Number (NaN).
[`isfinite`](https://numpy.org/doc/2.2/reference/generated/numpy.isfinite.html#numpy.isfinite "numpy.isfinite")
Shows which elements are finite (not NaN, not infinity)
Notes
NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic (IEEE 754). This means that Not a Number is not equivalent to infinity.
Examples
```
>>> 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. +0.j, 111111.+222222.j])
``` |
| Shard | 185 (laksa) |
| Root Hash | 448416686592400585 |
| Unparsed URL | org,numpy!/doc/2.2/reference/generated/numpy.nan_to_num.html s443 |