scipp.nan_to_num#
- scipp.nan_to_num(x, *, nan=None, posinf=None, neginf=None, out=None)#
Element-wise special value replacement.
All elements in the output are identical to input except in the presence of a NaN, Inf or -Inf. The function allows replacements to be separately specified for NaN, Inf or -Inf values. You can choose to replace a subset of those special values by providing just the required keyword arguments.
- Parameters:
x (
Variable) – Input data.nan (
Variable|None, default:None) – Replacement values for NaN in the input.posinf (
Variable|None, default:None) – Replacement values for Inf in the input.neginf (
Variable|None, default:None) – Replacement values for -Inf in the input.out (
Variable|None, default:None) – Optional output buffer.
- Raises:
scipp.DTypeError – If the types of input and replacement do not match.
- Returns:
Variable– Input with specified substitutions.
Examples
Replace special values with custom values:
>>> import scipp as sc >>> import numpy as np >>> x = sc.array(dims=['x'], values=[1.0, np.nan, np.inf, -np.inf, 2.0]) >>> sc.nan_to_num(x, nan=sc.scalar(0.0), posinf=sc.scalar(999.0), neginf=sc.scalar(-999.0)) <scipp.Variable> (x: 5) float64 [dimensionless] [1, 0, ..., -999, 2]
Replace only NaN values, leaving infinities unchanged:
>>> y = sc.array(dims=['x'], values=[1.0, np.nan, 2.0]) >>> sc.nan_to_num(y, nan=sc.scalar(-1.0)) <scipp.Variable> (x: 3) float64 [dimensionless] [1, -1, 2]