Version: v0.3.0

csv_reader

Stable type · Native arrays and readers

Tier: stable · Kind: type · Topic: Native arrays and readers

PythonJuliaRust
CSVReaderCSVReaderCsvReader

Python

class CSVReader
CSVReader.formats() -> list[str]
CSVReader.extensions() -> list[str]
CSVReader.open(blob_path: Any) -> Any
CSVReader.read_native(handle: Any, variables: Sequence[str] | None = None, select: Any | None = None, *, numeric_columns: Sequence[str] | None = None, delimiter: str | None = ',', header_row: int | None = 0, **_: Any) -> NativeDataset

The active csv reader — a non-NetCDF format behind the same registry.

Columns named in numeric_columns parse to float64 1-D arrays keyed by the column (file_variable) name; every other column is returned as a list of str. All fields carry the single dimension index; there are no coordinates.

numeric_columns is REQUIRED by the loader spec and is not inferred: the corpus location_id column is digit-only text ("1"/"2") yet must stay a string, so “parses as a number” is not a safe signal. When it is None the reader falls back to best-effort inference (every value parses as a float), which the loader/.esm node should override. Quoted fields with embedded delimiters are handled by csv; matches the Julia CSVReader.

Members

  • read_native — Decode a delimited-text blob into a NativeDataset of points.

Source

Example (provider)

source = DataSource(
    "stations",
    "csv",  # the format-registry key that selects the reader
    (FIXTURES / "stations.csv").as_uri(),
    variables=["value", "location_id"],
    reader_kwargs={"numeric_columns": ["latitude", "longitude", "value"]},
)
data = Provider(source, cache).materialize()
assert isinstance(data, NativeDataset)
assert data.variable_names() == ["location_id", "value"]
assert data["value"].data.dtype == np.float64
assert list(data["value"].data) == [152.3, 168.7, 98.1, 110.4]
# Digit-only text columns stay strings: nothing is inferred.
assert data["location_id"].data == ["1", "1", "2", "2"]

From docs/api/examples/python/provider.py

Julia

struct CSVReader <: Reader
CSVReader()
CSVReader()

The csv format reader — a second reader proving a non-NetCDF format plugs into FORMAT_REGISTRY unchanged (spec/conformance.md). Columns named in numeric_columns parse to Float64 1-D arrays keyed by the column (file_variable) name; every other column is returned as a String array. All fields have a single dimension index; there are no coordinates.

numeric_columns is REQUIRED by the loader spec and is not inferred: the corpus location_id column is digit-only text ("1","2") yet must stay a string, so “parses as a number” is not a safe signal. When numeric_columns === nothing the reader falls back to best-effort inference (every value parses as a float), which the loader/.esm node should override. Quoted fields with embedded delimiters are not handled (the points corpus has none); add that when a loader needs it.

header_row is the 0-based non-blank row holding the column names (default 0), the same index in every track; rows before it are dropped. The text is UTF-8 and a leading byte-order mark is dropped. A header that repeats a column name, or a numeric_columns entry the header lacks, is an error. A nothing option is unset.

Source

Example (provider)

url = "file://" * abspath(joinpath(FIXTURES, "stations.csv"))
p = const_provider(
    cache, url; format = "csv", source_loader = "stations",
    variables = ["value", "location_id"],
    reader_kwargs = (numeric_columns = ["latitude", "longitude", "value"],)
)
@assert is_const(p) && refresh_times(p) == Float64[]  # a CONST provider never refreshes
data = materialize(p)
@assert data isa NativeDataset
@assert Set(variable_names(data)) == Set(["location_id", "value"])
@assert data["value"].data == [152.3, 168.7, 98.1, 110.4]
# Digit-only text columns stay strings: nothing is inferred.
@assert data["location_id"].data == ["1", "1", "2", "2"]

From docs/api/examples/julia/provider.jl

Rust

pub struct CsvReader {
    /* private fields */
}

impl CsvReader { pub fn new() -> Self }

impl CsvReader { pub fn numeric_columns<I, S>(self, names: I) -> Self where I: IntoIterator<Item = S>, S: Into<String> }

impl CsvReader { pub fn delimiter(self, delimiter: u8) -> Self }

impl CsvReader { pub fn header_row(self, row: usize) -> Self }

The csv format reader. The default instance (no options) infers numeric columns, splits on , and reads the header from row 0.

Source · Rust docs