You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
statistics.variance() can return a negative result for data containing infinity #160034
statistics.variance([-math.inf, 1.0, 2.0]) returns -inf, a negative
variance. Since variance is a mean of squared deviations, a negative value is
impossible. variance() and pvariance() return a non-finite result for any
data containing inf or nan, instead of raising ValueError the way stdev() and pstdev() already do:
The result is also internally inconsistent: with an explicit xbar, variance([-math.inf, 1.0, 2.0], 0.0) returns +inf for the same data.
gh-140938 added the inf or nan encountered in data guard to stdev() and pstdev() but left variance() and pvariance() unguarded, even though stdev is the square root of variance.
Thanks for working on this! I was also looking into this issue. I'll review your patch and tests to understand the proposed fix. Please let us know when the PR is available so i can follow the discussion.
Reacted by tonghuaroot (童话) and Roshani Ajab Jadhao
Activity
tonghuaroot commentedon Oct 21, 2026
i have a patch ready for this, same guard as the stdev/pstdev one, plus tests:
main...tonghuaroot:gh-160034-variance-pvariance-inf-nan
my account can't open prs at the moment. i'll open the pr as soon as that's resolved.
jadhavroshani713-sys commentedon Oct 21, 2026
Thanks for working on this! I was also looking into this issue. I'll review your patch and tests to understand the proposed fix. Please let us know when the PR is available so i can follow the discussion.
NekoAsakura commentedon Oct 21, 2026
@hugovk bypass for @tonghuaroot? 54 merged, their past PRs look fine.
hugovk commentedon Oct 21, 2026
@tonghuaroot You're now in the bypass list.