Rate this Page
★ ★ ★ ★ ★

from_pandas#

tensordict.from_pandas(dataframe, *, auto_batch_size: bool = False, batch_dims: int | None = None, device: device | None = None, batch_size: Size | None = None, separator: str | None = None, dtype: dtype | None = None) → TensorDictBase#

Converts a pandas DataFrame to a tensordict, with one entry per column.

See TensorDictBase.from_pandas for how each column type is converted.

Parameters:

dataframe (pd.DataFrame) – the DataFrame to convert.

Keyword Arguments:
  • auto_batch_size (bool, optional) – if True, sets the batch size to the largest leading shape that all the entries share. Exclusive with batch_size. Defaults to False.

  • batch_dims (int, optional) – with auto_batch_size, the maximum number of batch dimensions. Ignored without auto_batch_size, with a FutureWarning when it would change the result. Starting with TensorDict 0.17, the default will change to turning on auto_batch_size when batch_dims is given, as from_dict does. Defaults to None.

  • device (device, str or int, optional) – the device of the result. Leaves on another device are moved to it. None leaves each leaf where it is, and the result has no device. Defaults to None.

  • batch_size (Size or list of int, optional) – the batch size of the result: the leading dimensions that all its entries share. Exclusive with auto_batch_size. Defaults to None, which gives [num_rows].

  • separator (str, optional) – if given, splits the column names on it to build nested entries: with separator=".", the column "obs.x" becomes the entry ("obs", "x"). Defaults to None.

  • dtype (dtype, optional) – if given, casts the numeric and boolean columns to it. Defaults to None.

Returns:

A TensorDict.

Examples

>>> import pandas as pd
>>> from tensordict import from_pandas
>>> df = pd.DataFrame({"obs.x": [1.0, 2.0], "obs.y": [3.0, 4.0], "done": [False, True]})
>>> td = from_pandas(df, separator=".")
>>> td["obs", "x"], td.batch_size
(tensor([1., 2.], dtype=torch.float64), torch.Size([2]))