Last modified: May 22, 2025 By Alexander Williams
Fix ValueError: Input Contains NaN or Infinity
Encountering a ValueError in Python can be frustrating. One common error is ValueError: Input contains NaN, infinity, or a value too large for dtype
. This article explains why it happens and how to fix it.
Table Of Contents
What Causes This ValueError?
This error occurs when your data contains invalid values. These include NaN (Not a Number), infinity, or extremely large numbers. Many Python functions, like those in NumPy or scikit-learn, reject such inputs.
For example, machine learning models cannot work with NaN or infinite values. They need clean, finite numbers for training and prediction.
How to Check for NaN or Infinity
Use numpy.isnan()
and numpy.isinf()
to detect problematic values. Here’s an example:
import numpy as np
data = [1, 2, np.nan, np.inf]
has_nan = np.isnan(data).any()
has_inf = np.isinf(data).any()
print("Has NaN:", has_nan)
print("Has Infinity:", has_inf)
Has NaN: True
Has Infinity: True
How to Fix the ValueError
Here are three ways to resolve this error:
1. Remove NaN or Infinite Values
Use numpy.nan_to_num()
to replace NaN and infinity with finite numbers. This function is useful for quick fixes.
import numpy as np
data = [1, 2, np.nan, np.inf]
clean_data = np.nan_to_num(data)
print("Cleaned Data:", clean_data)
Cleaned Data: [1. 2. 0. 1.79769313e+308]
2. Drop Rows with NaN
If you use pandas, dropna()
removes rows with NaN values. This keeps only valid data.
import pandas as pd
df = pd.DataFrame({"A": [1, 2, np.nan], "B": [3, np.nan, 5]})
clean_df = df.dropna()
print(clean_df)
A B
0 1.0 3.0
3. Replace NaN with Mean or Median
Another approach is filling NaN values. Use fillna()
in pandas to replace missing data.
import pandas as pd
df = pd.DataFrame({"A": [1, 2, np.nan], "B": [3, np.nan, 5]})
filled_df = df.fillna(df.mean())
print(filled_df)
A B
0 1.0 3.0
1 2.0 4.0
2 1.5 5.0
Preventing the Error
Always validate data before processing. Check for NaN or infinity early. This avoids errors in later stages.
For more on handling similar errors, see Fix ValueError: Operands Could Not Broadcast or Fix ValueError: x and y Dimension Mismatch.
Conclusion
The ValueError due to NaN or infinity is common but fixable. Clean your data using the methods above. For more Python error solutions, check our guide on Using try-except to Catch ValueError.