Datachain

ArrowRow

ArrowRow

Bases: DataModel

DataModel for reading row from Arrow-supported file.

open

open()

Stream row contents from indexed file.

Source code in datachain/lib/file.py
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
@contextmanager
def open(self):
    """Stream row contents from indexed file."""
    from pyarrow.csv import ParseOptions, ReadOptions
    from pyarrow.dataset import CsvFileFormat, dataset

    from datachain.lib.arrow import fix_pyarrow_format, pyarrow_fs

    kwargs = dict(self.kwargs)
    if csv := kwargs.pop("csv", None):
        parse = ParseOptions(**csv["parse_options"])
        if csv.get("skip_invalid_rows"):
            parse.invalid_row_handler = lambda _row: "skip"
        kwargs["format"] = fix_pyarrow_format(
            CsvFileFormat(
                read_options=ReadOptions(**csv["read_options"]),
                parse_options=parse,
            ),
            parse,
        )

    if self.file._caching_enabled:
        self.file.ensure_cached()
        path = self.file.get_local_path()
        ds = dataset(path, **kwargs)
    else:
        path = self.file.get_fs_path()
        fs = pyarrow_fs(self.file, kwargs.get("format"))
        ds = dataset(path, filesystem=fs, **kwargs)

    yield next(ds.take([self.index]).to_reader())

read

read()

Returns row contents as dict.

Best effort, with no guarantee to match the stored row: it parses the file again from the start up to this row, and not every reader option is kept, e.g. column types.

Source code in datachain/lib/file.py
2241
2242
2243
2244
2245
2246
2247
2248
2249
def read(self):
    """Returns row contents as dict.

    Best effort, with no guarantee to match the stored row: it parses the
    file again from the start up to this row, and not every reader option is
    kept, e.g. column types.
    """
    with self.open() as record_batch:
        return record_batch.to_pylist(maps_as_pydicts="strict")[0]