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:
| Name | Description |
|---|---|
round_float_columns | Round all Float64 columns to digits significant figures. |
round_sigfigs | Round array values to n significant figures. |
Functions¶
round_float_columns¶
round_float_columns(df: pl.DataFrame, digits: int = 4) -> pl.DataFrameRound 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:
| Name | Type | Description | Default |
|---|---|---|---|
df | DataFrame | Input DataFrame. | required |
digits | int | Number of significant figures. Must be >= 1. | 4 |
Returns:
| Type | Description |
|---|---|
DataFrame | New DataFrame with Float64 columns rounded. |
round_sigfigs¶
round_sigfigs(x: np.ndarray, n: int = 4) -> np.ndarrayRound array values to n significant figures.
Matches R’s signif() behavior: the n most significant digits
are preserved regardless of magnitude.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x | ndarray | Input array (float64). May contain NaN or inf values. | required |
n | int | Number of significant figures. Must be >= 1. | 4 |
Returns:
| Type | Description |
|---|---|
ndarray | Array of the same shape with values rounded to n significant |
ndarray | figures. 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])