from_arrow#
- tensordict.from_arrow(table: Any, *, copy: bool | None = None, nulls: Literal['raise', 'fill', 'mask'] = 'raise', mask_key: NestedKey | None = None, device: device | str | int | None = None) TensorDict#
Converts an Arrow table to a tensordict, with one entry per column, viewing the data where it can.
Requires pyarrow 15 or later. The columns convert as follows:
integer and floating-point columns become tensors that view the Arrow data through DLPack. Boolean columns are copied, since Arrow packs them as bits.
listandlarge_listcolumns of one of these types become jagged nested tensors (layout=torch.jagged) that view the values.fixed_size_listandarrow.fixed_shape_tensorcolumns become tensors with trailing dimensions.structcolumns become nested tensordicts.all the other columns, such as strings, binary, dictionaries, timestamps, and lists of lists or of strings, become a
NonTensorStackof the Python objects ofpyarrow.Array.to_pylist.
A column made of more than one chunk is first concatenated with
combine_chunks, which copies it. The tensors that view the Arrow data keep its buffers alive, and so the whole table that they come from.- Parameters:
table (pyarrow.Table or pyarrow.RecordBatch) – the table. Other objects go through
pyarrow.table()first: for example a Polars DataFrame, or any object that has an__arrow_c_stream__method. A Hugging Face dataset holds its table indataset.data.table: afterselect,shuffleorfilter, calldataset = dataset.flatten_indices()first, since this table holds all the rows of the original dataset.- Keyword Arguments:
copy (bool, optional) – whether to copy the data.
Noneviews the buffers that Arrow marks as mutable, and copies the immutable ones, such as those of a memory-mapped file (for example a Hugging Face dataset loaded from disk).Truecopies every column.Falseviews every column that it can: an in-place write into a view of an immutable buffer then crashes the process, since torch has no read-only tensors. Defaults toNone.nulls (str, optional) – what to do with null values.
"raise"raises aValueErrorfor a null row, and for a null inside the rows of a list or a struct. A null inside a map or a list view, or in the dictionary values that a dictionary-encoded column selects, comes back asNonewith aFutureWarning: starting with TensorDict 0.18, the default will change to raising aValueErrorfor these nulls too. Nulls inside a union or a run-end encoded column are not checked, and come back asNone."fill"fills them withNaNin floating-point tensors,0(False) in the other tensors, an empty list in jagged tensors, andNonein aNonTensorStack. A null struct makes each of its fields null."mask"fills them as"fill"does, and adds the boolean entry(*mask_key, *key)for each entrykeythat has nulls,Truewhere the row is not null. Filling copies the column. Defaults to"raise".mask_key (NestedKey, optional) – the key of the masks that
nulls="mask"adds. It differs from the"masks"key ofpad_sequence()withreturn_mask=True, so that the result can be padded with masks too. Passing it with anothernullsraises aValueError. Defaults to"_mask".device (device, str or int, optional) – the device of the result. Defaults to
None, which keeps the tensors on the CPU and gives a tensordict without a device.
- Returns:
A
TensorDictwith the batch size[num_rows].
See also
to_arrow(), the reverse conversion.Examples
>>> import pyarrow as pa >>> from tensordict import from_arrow >>> table = pa.table( ... { ... "x": pa.array([1.0, 2.0, 3.0]), ... "ids": pa.array([[1, 2], [3], []]), ... "text": pa.array(["a", "b", "c"]), ... } ... ) >>> td = from_arrow(table) >>> td["x"] tensor([1., 2., 3.], dtype=torch.float64) >>> td["ids"].unbind(0) (tensor([1, 2]), tensor([3]), tensor([], dtype=torch.int64)) >>> td.get("text").tolist() ['a', 'b', 'c'] >>> td = from_arrow(pa.table({"y": pa.array([1.0, None])}), nulls="mask") >>> td["y"], td["_mask", "y"] (tensor([1., nan], dtype=torch.float64), tensor([ True, False]))