Where loadtxt() fails immediately on a missing value or malformed row, genfromtxt() is specifically built to tolerate them: it can recognize designated missing-value markers, like an empty field or a specific string, and replace them with a specified filling value, commonly nan for numeric data, rather than crashing. This flexibility comes at a real performance cost — genfromtxt() does considerably more per-value work than loadtxt(), so for data you already know is complete and well-formed, loadtxt() remains the faster choice.
1Understanding np.genfromtxt()
Where loadtxt() fails immediately on a missing value or malformed row, genfromtxt() is specifically built to tolerate them: it can recognize designated missing-value markers, like an empty field or a specific string, and replace them with a specified filling value, commonly nan for numeric data, rather than crashing. This flexibility comes at a real performance cost — genfromtxt() does considerably more per-value work than loadtxt(), so for data you already know is complete and well-formed, loadtxt() remains the faster choice.
Use genfromtxt() specifically when a text file might have missing or malformed values that need graceful handling — but don't reach for it by default for clean data, since it's noticeably slower than loadtxt() due to all the extra validation and handling it performs per value.
import numpy as np
with open("messy.csv", "w") as f:
f.write("1,2,3\n4,,6\n7,8,9\n")
data = np.genfromtxt("messy.csv", delimiter=",")
print(data)2Practical Example
Here is a real-world application of np.genfromtxt() showing how it is used in production NumPy code.
import numpy as np
with open("messy.csv", "w") as f:
f.write("1,2,3\n4,,6\n7,8,9\n")
data = np.genfromtxt("messy.csv", delimiter=",", filling_values=0)
print(data)3Best Practices
Follow these guidelines when working with np.genfromtxt():
1. Use genfromtxt() specifically when missing values or inconsistent rows are a realistic possibility in the input file
2. Prefer the faster loadtxt() for data you already know is clean and complete, reserving genfromtxt()'s extra robustness for messier real-world sources
3. Configure missing_values and filling_values deliberately to match how missing data is actually represented in your specific file, rather than assuming the defaults will catch everything
Tip: Use genfromtxt() specifically when a text file might have missing or malformed values that need graceful handling — but don't reach for it by default for clean data, since it's noticeably slower than loadtxt() due to all the extra validation and handling it performs per value.
import numpy as np
with open("messy.csv", "w") as f:
f.write("1,2,3\n4,,6\n7,8,9\n")
data = np.genfromtxt("messy.csv", delimiter=",")
print(data)