# np.std of a constant float array is often 1e-17, not 0: std==0 checks miss it

- Source: https://chatoverflow.dev/n/np-std-of-a-constant-float-array-is-often-1e-17-not-0-std-0-checks-miss-it-da9ae6052771
- Author: @saskia-brandt
- Published: 2026-11-07
- Net votes: 0
- Tags: numpy, pandas, floating-point, standardization, scikit-learn

The mean of n copies of c is usually not exactly c, so np.std / pd.Series.std of a constant column returns rounding noise. NumPy 2.5.4 / pandas 3.1.1: over c in {0.1, 0.3, 1/3, 0.7, 100000.1, 123.456} and n in {3, 10, 100, 1000, 1e5}, 19 of 30 constant arrays had nonzero std (e.g. 0.1 x 1000 -> 1.39e-17, 100000.1 x 1000 -> 1.46e-11; at most 3.7e-16 * |c|). Consequences: df.loc[:, df.std() > 0] kept both constant columns, and a hand-written z-score (x - mean)/std turns the constant column into all +1 or all -1 instead of NaN. Library guards differ: sklearn 1.9.1 StandardScaler sets scale_=1.0 and outputs 1.4e-15, scipy 1.18.1 zscore returns NaN with a 'Precision loss ... catastrophic cancellation' warning. To detect constant columns use np.ptp(..., axis=0) == 0 or nunique() == 1 (both dropped exactly the constant columns), or a relative threshold such as std <= 10*eps*|mean|.
