to_arrow#
- tensordict.to_arrow(td: TensorDictBase) pyarrow.Table#
Converts a tensordict with one batch dimension to an Arrow table, with one column per entry.
Requires pyarrow 15 or later. This is the reverse of
from_arrow(). The entries convert as follows:1-D tensors become primitive columns. The columns of contiguous numeric tensors on the CPU view their data. Boolean tensors are copied.
tensors with more dimensions become
arrow.fixed_shape_tensorcolumns.jagged nested tensors (
layout=torch.jagged) with their ragged dim at position 1 becomelarge_listcolumns; another ragged dim raises aValueError.nested tensordicts become
structcolumns.non-tensor entries, such as a
NonTensorStackof strings, become columns of the type thatpyarrow.array()infers from their values.
A
LazyStackedTensorDictwith entries of different shapes converts afterdensify(layout=torch.jagged).- Parameters:
td (TensorDictBase) – the tensordict. Its batch size must have one dimension: call
td.reshape(-1)first to flatten a batch with more dimensions.- Returns:
A
pyarrow.Tablewithtd.batch_size[0]rows, or no rows iftdhas no entries.
Examples
>>> import torch >>> from tensordict import TensorDict, to_arrow >>> td = TensorDict( ... x=torch.tensor([1.0, 2.0]), ... image=torch.zeros(2, 3, 4), ... obs=TensorDict(y=torch.tensor([3, 4]), batch_size=[2]), ... batch_size=[2], ... ) >>> to_arrow(td).schema x: float image: extension<arrow.fixed_shape_tensor[value_type=float, shape=[3,4], permutation=[0,1]]> obs: struct<y: int64> child 0, y: int64