Shortcuts

torcharrow.dataframe

torcharrow.dataframe(data: Optional[Union[Iterable, DType]] = None, dtype: Optional[DType] = None, columns: Optional[List[str]] = None, device: str = '')

创建一个 TorchArrow 数据框。

Parameters:
  • data (dict元组列表) – 定义DataFrame的内容。字典的键用于列名,值用于列。使用dtype来强制特定的列顺序。当Data是元组列表时,必须提供dtype以推断字段名称。

  • dtype (dtype, 默认 None) – 强制指定的数据类型。如果为 None,则尽可能自动推断类型。应该是一个 dt.Struct(),提供一个 dt.Fields 的列表。

  • columns (字符串列表, 默认 None) – 列的名称。当数据是没有提供自定义dtype的元组列表时使用。当数据和dtype都为None时,应将其保留为None(语义是构造一个没有任何列的默认空DataFrame)。

  • device (Device, 默认 "") – 设备选择从范围中使用哪个运行时。TorchArrow 支持多个运行时(CPU 和 GPU)。如果未提供,则使用 Velox 向量化运行时。有效值为“cpu”(Velox)、“gpu”(即将推出)。

示例

数据框只是一组具有相同长度的命名和强类型列:

>>> import torcharrow as ta
>>> df = ta.dataframe({'a': list(range(7)),
>>>                    'b': list(reversed(range(7))),
>>>                    'c': list(range(7))
>>>                   })
>>> df
  index    a    b    c
-------  ---  ---  ---
      0    0    6    0
      1    1    5    1
      2    2    4    2
      3    3    3    3
      4    4    2    4
      5    5    1    5
      6    6    0    6
dtype: Struct([Field('a', int64), Field('b', int64), Field('c', int64)]), count: 7, null_count: 0

DataFrames 是不可变的,除非你可以添加一个新列,前提是其名称尚未被使用。该列将附加到现有列的末尾:

>>> df['d'] = ta.column(list(range(99, 99+7)))
>>> df
  index    a    b    c    d
-------  ---  ---  ---  ---
      0    0    6    0   99
      1    1    5    1  100
      2    2    4    2  101
      3    3    3    3  102
      4    4    2    4  103
      5    5    1    5  104
      6    6    0    6  105
dtype: Struct([Field('a', int64), Field('b', int64), Field('c', int64), Field('d', int64)]), count: 7, null_count: 0

构建嵌套数据框:

>>> df_inner = ta.dataframe({'b1': [11, 22, 33], 'b2':[111,222,333]})
>>> df_outer = ta.dataframe({'a': [1, 2, 3], 'b':df_inner})
>>> df_outer
  index    a  b
-------  ---  ---------
      0    1  (11, 111)
      1    2  (22, 222)
      2    3  (33, 333)
dtype: Struct([Field('a', int64), Field('b', Struct([Field('b1', int64), Field('b2', int64)]))]), count: 3, null_count: 0

从元组列表构建一个Dataframe:

>>> import torcharrow.dtypes as dt
>>> l = [(1, 'a'), (2, 'b'), (3, 'c')]
>>> ta.dataframe(l, dtype = dt.Struct([dt.Field('t1', dt.int64), dt.Field('t2', dt.string)]))
  index    t1  t2
-------  ----  ----
      0     1  a
      1     2  b
      2     3  c
dtype: Struct([Field('t1', int64), Field('t2', string)]), count: 3, null_count: 0

>>> ta.dataframe(l, columns=['t1', 't2'])
  index    t1  t2
-------  ----  ----
      0     1  a
      1     2  b
      2     3  c
dtype: Struct([Field('t1', int64), Field('t2', string)]), count: 3, null_count: 0