Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Significant-figures rounding for display output.

Provides vectorized rounding to N significant figures, matching R’s signif() behavior. Used at the user-facing API boundary to produce clean DataFrame output without altering internal computation precision.

Functions:

NameDescription
round_float_columnsRound all Float64 columns to digits significant figures.
round_sigfigsRound array values to n significant figures.

Functions

round_float_columns

round_float_columns(df: pl.DataFrame, digits: int = 4) -> pl.DataFrame

Round all Float64 columns to digits significant figures.

Non-float columns (String, Int32, Int64, etc.) are passed through unchanged. Null values within float columns are preserved.

Parameters:

NameTypeDescriptionDefault
dfDataFrameInput DataFrame.required
digitsintNumber of significant figures. Must be >= 1.4

Returns:

TypeDescription
DataFrameNew DataFrame with Float64 columns rounded.

round_sigfigs

round_sigfigs(x: np.ndarray, n: int = 4) -> np.ndarray

Round array values to n significant figures.

Matches R’s signif() behavior: the n most significant digits are preserved regardless of magnitude.

Parameters:

NameTypeDescriptionDefault
xndarrayInput array (float64). May contain NaN or inf values.required
nintNumber of significant figures. Must be >= 1.4

Returns:

TypeDescription
ndarrayArray of the same shape with values rounded to n significant
ndarrayfigures. NaN and inf values are passed through unchanged.

Examples:

>>> round_sigfigs(np.array([0.00012345, 3.14159, 12345.68]), n=4)
array([1.235e-04, 3.142e+00, 1.235e+04])