Shortcuts

torcharrow.Column.map

Column.map(arg: ty.Union[ty.Dict, ty.Callable], na_action: ty.Literal['ignore', None] = None, dtype: ty.Optional[dt.DType] = None, columns: ty.Optional[ty.List[str]] = None)

根据输入对应关系映射行。

Parameters:
  • callable (arg - dict 或) – 如果 arg 是一个字典,则使用此字典映射输入,未映射的值将变为 null。如果 arg 是一个可调用对象,则将其视为用户定义的函数 (UDF),该函数将在输入的每个元素上调用。可调用对象必须是全局函数或类实例上的方法,不支持 lambda 函数。

  • None (默认) – 如果你的UDF对空输入返回空值,选择“忽略”是一种效率提升,其中map将避免在空值上调用你的UDF。如果为None,则始终调用UDF。

  • None – 如果你的UDF对空输入返回null,选择“忽略”是一种效率提升,其中map将避免在空值上调用你的UDF。如果为None,则始终调用UDF。

  • DType (dtype -) – DType 用于强制输出类型。如果结果类型 != 项目类型,则需要 DType。

  • None – DType 用于强制输出类型。如果结果类型 != 项目类型,则需要 DType。

  • names (columns - 列的列表) – 确定哪些列提供给映射字典或UDF。

  • None – 确定哪些列提供给映射字典或UDF。

另请参阅

flatmap, filter

示例

>>> import torcharrow as ta
>>> ta.column([1,2,None,4]).map({1:111})
0  111
1  None
2  None
3  None
dtype: Int64(nullable=True), length: 4, null_count: 3

使用defaultdict来提供一个缺失值:

>>> from collections import defaultdict
>>> ta.column([1,2,None,4]).map(defaultdict(lambda: -1, {1:111}))
0  111
1   -1
2   -1
3   -1
dtype: Int64(nullable=True), length: 4, null_count: 0

使用用户提供的python函数:

>>> def add_ten(num):
>>>     return num + 10
>>>
>>> ta.column([1,2,None,4]).map(add_ten, na_action='ignore')
0  11
1  12
2  None
3  14
dtype: Int64(nullable=True), length: 4, null_count: 1

请注意,在上面的示例中,.map(add_ten, na_action=None) 会因为 add_ten 未定义 None/null 而失败并抛出类型错误。要将空值传递给 UDF,UDF 需要为此做好准备:

>>> def add_ten_or_0(num):
>>>     return 0 if num is None else num + 10
>>>
>>> ta.column([1,2,None,4]).map(add_ten_or_0, na_action=None)
0  11
1  12
2   0
3  14
dtype: Int64(nullable=True), length: 4, null_count: 0

映射到不同类型需要一个dtype参数:

>>> ta.column([1,2,None,4]).map(str, dtype=dt.string)
0  '1'
1  '2'
2  'None'
3  '4'
dtype: string, length: 4, null_count: 0

在DataFrame上进行映射时,UDF将整行作为元组获取:

>>> def add_unary(tup):
>>>     return tup[0]+tup[1]
>>>
>>> ta.dataframe({'a': [1,2,3], 'b': [1,2,3]}).map(add_unary , dtype = dt.int64)
0  2
1  4
2  6
dtype: int64, length: 3, null_count: 0

多参数用户定义函数:

>>> def add_binary(a,b):
>>>     return a + b
>>>
>>> ta.dataframe({'a': [1,2,3], 'b': ['a', 'b', 'c'], 'c':[1,2,3]}).map(add_binary, columns = ['a','c'], dtype = dt.int64)
0  2
1  4
2  6
dtype: int64, length: 3, null_count: 0

多返回UDFs - 返回多列的函数可以通过返回一个DataFrame(也称为结构列)来指定;必须提供返回的数据类型:

>>> ta.dataframe({'a': [17, 29, 30], 'b': [3,5,11]}).map(divmod, columns= ['a','b'], dtype = dt.Struct([dt.Field('quotient', dt.int64), dt.Field('remainder', dt.int64)]))
  index    quotient    remainder
-------  ----------  -----------
      0           5            2
      1           5            4
      2           2            8
dtype: Struct([Field('quotient', int64), Field('remainder', int64)]), count: 3, null_count: 0

可以通过在(数据)类中捕获状态并使用方法作为委托来编写具有状态的UDFs:

>>> def fib(n):
>>>     if n == 0:
>>>         return 0
>>>     elif n == 1 or n == 2:
>>>         return 1
>>>     else:
>>>         return fib(n-1) + fib(n-2)
>>>
>>> from dataclasses import dataclass
>>> @dataclass
>>> class State:
>>>     state: int
>>>     def __post_init__(self):
>>>         self.state = fib(self.state)
>>>     def add_fib(self, x):
>>>         return self.state+x
>>>
>>> m = State(10)
>>> ta.column([1,2,3]).map(m.add_fib)
0  56
1  57
2  58
dtype: int64, length: 3, null_count: 0