csv_reader
Stable type · Native arrays and readers
Tier: stable · Kind: type · Topic: Native arrays and readers
| Python | Julia | Rust |
|---|---|---|
CSVReader | CSVReader | not exported |
Python
class CSVReader
CSVReader.formats() -> List[str]
CSVReader.extensions() -> List[str]
CSVReader.open(blob_path: Any) -> Any
CSVReader.read_native(handle: Any, variables: Optional[Sequence[str]] = None, select: Optional[Any] = None, *, numeric_columns: Optional[Sequence[str]] = None, delimiter: str = ',', header_row: int = 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 aNativeDatasetof points.
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"]
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.
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"]
Rust
Rust does not export this type.