maxframe.dataframe.Series.mf.apply_chunk#

Series.mf.apply_chunk(func: str | Callable, batch_rows=None, dtypes=None, dtype=None, name=None, output_type=None, index=None, skip_infer=False, args=(), check_output_dtypes=None, **kwargs)#

应用一个函数,该函数接收 pandas Series 并输出 pandas DataFrame/Series。提供给该函数的 pandas DataFrame 是输入 series 的一个区块。

传递给此函数的对象是原始 series 的切片,包含最多 batch_rows 数量的元素。函数输出可以是 DataFrame 或 Series。apply_chunk 最终会将结果合并到一个新的 DataFrame 或 Series 中。

不要期望在函数中接收到 series 的所有元素,因为它依赖于 MaxFrame 的实现和 MaxCompute 的内部运行状态。

可以是 ufunc(应用于整个 Series 的 NumPy 函数)或仅适用于 series 的 Python 函数。

参数:
  • func (function) -- 要应用的 Python 函数或 NumPy ufunc。

  • batch_rows (int) -- 指定批次中预期的元素数量,以及函数输入 series 的长度。当剩余数据不足时,可能少于这个数量。

  • output_type ({'dataframe', 'series'}, default None) -- 指定返回对象的类型。详见 备注 部分。

  • dtypes (Series, default None) -- 指定返回 DataFrames 的 dtypes。详见 备注 部分。

  • dtype (numpy.dtype, default None) -- 指定返回 Series 的 dtype。详见 备注 部分。

  • name (str, default None) -- 指定返回 Series 的名称。详见 备注 部分。

  • index (Index, default None) -- 指定返回对象的索引。详见 备注 部分。

  • args (tuple) -- 在 series 值之后传递给 func 的位置参数。

  • skip_infer (bool, default False) -- 当未指定 dtypes 或 output_type 时是否推断 dtypes。

  • check_output_dtypes (str, default None) -- 输出数据类型和列的验证模式:- 'ignore':不执行验证 - 'warns':验证并在不匹配时显示警告(None 时的默认值)- 'raises':验证并在不匹配时引发错误

  • **kwds -- 传递给 func 的额外关键字参数。

返回:

如果 func 返回 Series 对象,则结果将是 Series,否则结果将是 DataFrame。

返回类型:

Series or DataFrame

参见

DataFrame.apply_chunk

对 DataFrame 区块应用函数。

Series.apply

用于非批处理操作。

备注

当决定输出 dtypes 和返回值形状时,MaxFrame 将尝试将 func 应用到一个模拟 Series 上,apply 调用可能会失败。当这种情况发生时,您需要在 output_type 中指定 apply 调用的类型(DataFrame 或 Series)。

  • 对于 DataFrame 输出,您需要指定一个列表或 pandas Series 作为输出 DataFrame 的 dtypes。也可以指定输出的 index

  • 对于 Series 输出,您需要指定输出 Series 的 dtypename

  • 对于任何数据类型为 pandas.ArrowDtype(pyarrow.MapType) 的输入,它将始终被转换为 Python dict。对于任何具有此数据类型的输出,也必须作为 Python dict 返回。

示例

为每个城市创建一个典型的夏季温度 series。

>>> import maxframe.tensor as mt
>>> import maxframe.dataframe as md
>>> s = md.Series([20, 21, 12],
...               index=['London', 'New York', 'Helsinki'])
>>> s.execute()
London      20
New York    21
Helsinki    12
dtype: int64

通过定义一个函数并将其作为参数传递给 apply_chunk() 来对值进行平方。

>>> def square(x):
...     return x ** 2
>>> s.mf.apply_chunk(square, batch_rows=2).execute()
London      400
New York    441
Helsinki    144
dtype: int64

通过将匿名函数作为参数传递给 apply_chunk() 来对值进行平方。

>>> s.mf.apply_chunk(lambda x: x**2, batch_rows=2).execute()
London      400
New York    441
Helsinki    144
dtype: int64

定义一个需要额外位置参数的自定义函数,并使用 args 关键字传递这些额外参数。

>>> def subtract_custom_value(x, custom_value):
...     return x - custom_value
>>> s.mf.apply_chunk(subtract_custom_value, args=(5,), batch_rows=3).execute()
London      15
New York    16
Helsinki     7
dtype: int64

定义一个接受关键字参数的自定义函数,并将这些参数传递给 apply_chunk

>>> def add_custom_values(x, **kwargs):
...     for month in kwargs:
...         x += kwargs[month]
...     return x
>>> s.mf.apply_chunk(add_custom_values, batch_rows=2, june=30, july=20, august=25).execute()
London      95
New York    96
Helsinki    87
dtype: int64

如果 func 返回一个 dataframe,apply_chunk 也会返回一个 dataframe。

>>> def get_dataframe(x):
...     return pd.concat([x, x], axis=1)
>>> s.mf.apply_chunk(get_dataframe, batch_rows=2).execute()
           0   1
London    20  20
New York  21  21
Helsinki  12  12

提供 dtypes 或带有名称的 dtype 来命名输出模式。

>>> s.mf.apply_chunk(
...    get_dataframe,
...    batch_rows=2,
...    dtypes={"A": np.int_, "B": np.int_},
...    output_type="dataframe"
... ).execute()
           A   B
London    20  20
New York  21  21
Helsinki  12  12

创建一个字典类型的 series。

>>> import pyarrow as pa
>>> from maxframe.lib.dtypes_extension import dict_
>>> s = md.Series(
...     data=[[("k1", 1), ("k2", 2)], [("k1", 3)], None],
...     index=[1, 2, 3],
...     dtype=dict_(pa.string(), pa.int64()),
... )
>>> s.execute()
1    [('k1', 1), ('k2', 2)]
2               [('k1', 3)]
3                      <NA>
dtype: map<string, int64>[pyarrow]

定义一个函数,该函数批量更新映射类型并添加新的键值对。

>>> def custom_set_item(row):
...     for _, value in row.items():
...         if value is not None:
...             value["x"] = 100
...     return row
>>> s.mf.apply_chunk(
...     custom_set_item,
...     output_type="series",
...     dtype=s.dtype,
...     batch_rows=2,
...     skip_infer=True,
...     index=s.index,
... ).execute()
1    [('k1', 1), ('k2', 2), ('x', 100)]
2               [('k1', 3), ('x', 100)]
3                                  <NA>
dtype: map<string, int64>[pyarrow]