Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Replace NaN with zero and infinity with large finite numbers in Python
In Python, NaN (Not a Number) and infinity values can cause issues in numerical computations. NumPy provides nan_to_num() to replace these non-finite values with usable finite numbers.
Syntax
numpy.nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None)
Parameters
The function accepts the following parameters:
- x ? Input array containing the data
- copy ? Whether to create a copy (True) or replace in-place (False). Default is True
- nan ? Value to replace NaN with. Default is 0.0
- posinf ? Value to replace positive infinity with. Default is very large finite number
- neginf ? Value to replace negative infinity with. Default is very small finite number
Basic Example
Let's create an array with NaN and infinity values and replace them ?
import numpy as np
# Create array with non-finite values
data = np.array([np.inf, -np.inf, np.nan, -128, 128])
print("Original Array:")
print(data)
# Replace non-finite values
result = np.nan_to_num(data)
print("\nAfter nan_to_num():")
print(result)
Original Array: [ inf -inf nan -128. 128.] After nan_to_num(): [ 1.79769313e+308 -1.79769313e+308 0.00000000e+000 -1.28000000e+002 1.28000000e+002]
Custom Replacement Values
You can specify custom values for NaN and infinity replacements ?
import numpy as np
data = np.array([np.inf, -np.inf, np.nan, 5.0, -3.0])
print("Original Array:")
print(data)
# Custom replacement values
result = np.nan_to_num(data, nan=-999, posinf=1000, neginf=-1000)
print("\nWith custom values:")
print(result)
Original Array: [ inf -inf nan 5. -3.] With custom values: [ 1000. -1000. -999. 5. -3.]
In-Place Replacement
Use copy=False to modify the original array directly ?
import numpy as np
data = np.array([np.nan, np.inf, 42.0])
print("Before in-place replacement:")
print(data)
# Modify original array
np.nan_to_num(data, copy=False)
print("\nAfter in-place replacement:")
print(data)
Before in-place replacement: [ nan inf 42.] After in-place replacement: [ 0.00000000e+000 1.79769313e+308 4.20000000e+001]
Practical Use Case
Common scenario when cleaning data before mathematical operations ?
import numpy as np
# Simulated data with problematic values
temperatures = np.array([25.3, np.nan, np.inf, -np.inf, 30.1])
print("Raw temperature data:")
print(temperatures)
# Clean the data
clean_temps = np.nan_to_num(temperatures, nan=25.0, posinf=50.0, neginf=-50.0)
print("\nCleaned temperature data:")
print(clean_temps)
# Now safe to calculate statistics
print(f"\nMean temperature: {np.mean(clean_temps):.2f}")
print(f"Max temperature: {np.max(clean_temps):.2f}")
Raw temperature data: [25.3 nan inf -inf 30.1] Cleaned temperature data: [ 25.3 25. 50. -50. 30.1] Mean temperature: 16.08 Max temperature: 50.00
Conclusion
The numpy.nan_to_num() function is essential for cleaning data with NaN and infinity values. Use custom replacement values for domain-specific cleaning, and consider in-place operations for memory efficiency.
Advertisements
