Skip to content

How to Resolve "RuntimeWarning: invalid value encountered in double_scalars" in Python

How to fix the NumPy warning for invalid floating-point operations like division by zero, empty array means, or NaN values with practical solutions.

When performing numerical computations with NumPy, you may encounter the warning RuntimeWarning: invalid value encountered in double_scalars. This warning occurs when a mathematical operation produces an undefined or invalid result such as dividing zero by zero, computing the mean of an empty array, or operating on NaN or infinite values.

In this guide, we'll explain what causes this warning, show common scenarios that trigger it, and walk through multiple solutions to handle it properly.

What Does This Warning Mean?

The term "double_scalars" refers to 64-bit floating-point (double-precision) scalar values. The warning is telling you that an operation on these values produced an invalid result, typically NaN (Not a Number) or inf (infinity).

Common operations that trigger this warning:

Operation Result Why
0.0 / 0.0 NaN Division is mathematically undefined
np.inf / np.inf NaN Indeterminate form
np.inf - np.inf NaN Indeterminate form
np.mean([]) NaN Mean of empty array is undefined
np.log(0) -inf Logarithm of zero is undefined
np.sqrt(-1) NaN Square root of negative (in reals)

Common Scenarios and Solutions

Scenario 1: Mean of an Empty Array

Computing the mean of an empty array or list triggers the warning because there are no elements to average.

❌ Warning: Mean of an empty array:

import numpy as np

data = []
result = np.mean(data)
print(result)

Output:

RuntimeWarning: Mean of empty slice.
RuntimeWarning: invalid value encountered in scalar divide
nan

✅ Fix: Validate input before computing:

import numpy as np

data = []

if len(data) > 0:
    result = np.mean(data)
    print(f"Mean: {result}")
else:
    print("Cannot compute mean of an empty array.")

Output:

Cannot compute mean of an empty array.

Scenario 2: Division by Zero

Dividing zero by zero (or inf by inf) produces NaN with a warning.

❌ Warning: Zero divided by zero:

import numpy as np

a = np.float64(0.0)
b = np.float64(0.0)

result = a / b
print(result)

Output:

RuntimeWarning: invalid value encountered in scalar divide
nan

✅ Fix: Check the denominator before dividing:

import numpy as np

a = np.float64(0.0)
b = np.float64(0.0)

if b != 0:
    result = a / b
else:
    result = 0.0  # Or np.nan, or any default that makes sense
    print("Warning: Division by zero encountered. Using default value.")

print(f"Result: {result}")

Output:

Warning: Division by zero encountered. Using default value.
Result: 0.0

For array-wise safe division, use np.divide() with the where parameter:

import numpy as np

numerator = np.array([10, 0, 30, 0])
denominator = np.array([2, 0, 5, 0])

result = np.divide(
    numerator,
    denominator,
    out=np.zeros_like(numerator, dtype=float),
    where=denominator != 0
)
print(result)

Output:

[5. 0. 6. 0.]

Scenario 3: Arrays Containing NaN Values

When your data contains NaN values, standard NumPy functions propagate the NaN through calculations.

❌ Warning: Mean with NaN values:

import numpy as np

data = np.array([10, 20, np.nan, 40, 50])
print(np.mean(data))

Output:

nan

No warning here, but the result is NaN, which is likely not what you want.

✅ Fix: Use NaN-safe functions:

NumPy provides nan-prefixed versions of most aggregation functions that ignore NaN values:

import numpy as np

data = np.array([10, 20, np.nan, 40, 50])

print(f"nanmean: {np.nanmean(data)}")
print(f"nansum:  {np.nansum(data)}")
print(f"nanstd:  {np.nanstd(data)}")
print(f"nanmax:  {np.nanmax(data)}")
print(f"nanmin:  {np.nanmin(data)}")

Output:

nanmean: 30.0
nansum:  120.0
nanstd:  15.811388300841896
nanmax:  50.0
nanmin:  10.0
Standard Function NaN-Safe Alternative
np.mean() np.nanmean()
np.sum() np.nansum()
np.std() np.nanstd()
np.var() np.nanvar()
np.max() np.nanmax()
np.min() np.nanmin()
np.median() np.nanmedian()

Scenario 4: Overflow with Large Exponentials

Operations involving very large numbers can overflow to inf, and subsequent operations on inf can produce NaN.

❌ Warning: Overflow in exponential calculation:

import numpy as np

x = 900
y = 711

result = np.exp(x) / np.exp(y)
print(result)

Output:

RuntimeWarning: overflow encountered in exp
RuntimeWarning: invalid value encountered in scalar divide
inf

✅ Fix: Use scipy.special.logsumexp for numerically stable computation:

from scipy.special import logsumexp

x = 900
y = 711

result = logsumexp([x]) - logsumexp([y])
print(result)

Output:

189.0

For ratios of exponentials, you can also simplify mathematically:

import numpy as np

x = 900
y = 711

result = np.exp(x - y)
print(result)

Output:

1.2068605179340022e+82

Scenario 5: Extreme Values Causing Instability

When array values are at the edge of floating-point representable range, use np.clip() to constrain them:

import numpy as np

values = np.array([1e300, -1e309, 1e308, 0, -1e300])

safe_values = np.clip(values, -1e308, 1e308)
print(safe_values)

print(f"Mean: {np.mean(safe_values)}")

Output:

[ 1.e+300 -1.e+308  1.e+308  0.e+000 -1.e+300]
Mean: -8.954221016089819e+287

Suppressing the Warning (Use with Caution)

If you've already handled invalid values in your logic and want to suppress the warning, you can use np.errstate():

import numpy as np

with np.errstate(invalid='ignore', divide='ignore'):
    result = np.float64(0.0) / np.float64(0.0)
    print(result)  # nan: no warning printed

Or suppress it globally (not recommended for production):

import warnings
warnings.filterwarnings('ignore', category=RuntimeWarning)

Detecting and Cleaning Invalid Values

Before performing calculations, check for and handle invalid values:

import numpy as np

data = np.array([10, np.nan, np.inf, -np.inf, 30, 0])

print(f"Has NaN:  {np.any(np.isnan(data))}")
print(f"Has Inf:  {np.any(np.isinf(data))}")
print(f"All finite: {np.all(np.isfinite(data))}")

clean_data = data[np.isfinite(data)]
print(f"\nClean data: {clean_data}")
print(f"Mean: {np.mean(clean_data)}")

Output:

Has NaN:  True
Has Inf:  True
All finite: False

Clean data: [10.  30.   0.]
Mean: 13.333333333333334

Quick Reference: Common Fixes

Cause Fix
Mean of empty array Check len(arr) > 0 before computing
0 / 0 or inf / inf Check denominator; use np.divide() with where
NaN values in data Use np.nanmean(), np.nansum(), etc.
Overflow with large exponentials Use scipy.special.logsumexp or simplify math
Extreme values Use np.clip() to constrain range
Mixed invalid values Filter with np.isfinite()
Known edge case: suppress warning Use np.errstate(invalid='ignore') locally

Conclusion

The RuntimeWarning: invalid value encountered in double_scalars warning occurs when a floating-point operation produces a mathematically undefined result like NaN or inf.

The best approach is to prevent the invalid operation in the first place: validate inputs, check for empty arrays, guard against division by zero, and use NaN-safe functions like np.nanmean(). For numerical stability with large values, use scipy.special.logsumexp or np.clip().

Only suppress the warning with np.errstate() when you've already handled the edge cases and are confident the invalid values won't affect downstream calculations.

Navigation