CSV files often arrive as plain tables before their values are ready for array calculations. Reading one into NumPy works well when the rows and columns are regular enough to become numeric columns or a small structured array.
np.genfromtxt() reads delimited text into an array and can treat a header row as field names. It can also turn blank numeric cells into NaN so later checks such as np.isnan() and np.nanmean() behave predictably.
Use np.loadtxt() for clean numeric-only files, and use np.genfromtxt() when the CSV has headers, selected columns, or missing values. For quoted text, complex dates, or irregular rows, inspect the parsed table first or clean the file with a CSV-aware tool before relying on the NumPy array.
Related: Write CSV data
Related: Convert array dtype
Related: Save and load NPY
Tool: Comma-Separated Values (CSV) Converter
sensor,temp,humidity A,21.5,45 B,,52 C,19.0,49
The blank field in row B gives the read step a missing numeric value to verify.
from pathlib import Path import numpy as np path = Path("readings.csv") data = np.genfromtxt( path, delimiter=",", names=True, dtype=None, encoding=None, missing_values="", filling_values=np.nan, ) print("rows:", data.shape[0]) print("columns:", data.dtype.names) print("temps:", data["temp"]) print("missing temp:", np.isnan(data["temp"][1])) print("average temp:", np.nanmean(data["temp"]))
dtype=None lets NumPy infer each named column independently. missing_values=“” and filling_values=np.nan convert the blank temperature cell into a numeric NaN.
$ python3 csv-read.py
rows: 3
columns: ('sensor', 'temp', 'humidity')
temps: [21.5 nan 19. ]
missing temp: True
average temp: 20.25
The output proves that the header row became named fields and that the blank value stayed in the numeric temperature column as NaN.
$ rm readings.csv csv-read.py
Keep real input files in place; this cleanup removes only the temporary files created for the local test.
Related: Write CSV data