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
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
@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

    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()
        ds = dataset(path, filesystem=self.file.get_fs(), **kwargs)

    return 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
2099
2100
2101
2102
2103
2104
2105
2106
2107
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()[0]