maxframe.dataframe.Series.apply#

Series.apply(func, convert_dtype=True, output_type=None, args=(), dtypes=None, dtype=None, name=None, index=None, skip_infer=False, check_output_dtypes=None, **kwds)#

对 Series 的值调用函数。

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

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

  • convert_dtype (bool, default True) -- 尝试为逐元素函数结果找到更好的 dtype。如果为 False,则保留为 dtype=object。

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

  • dtypes (Series, default None) -- 指定返回 DataFrame 的 dtypes。详见 Notes

  • dtype (numpy.dtype, default None) -- 指定返回 Series 的 dtype。详见 Notes

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

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

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

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

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

  • **kwds -- 传递给 func 的其他关键字参数。

返回:

如果 func 返回一个 Series 对象,结果将是一个 DataFrame。

返回类型:

Series or DataFrame

参见

Series.map

用于逐元素操作。

Series.agg

仅执行聚合类型操作。

Series.transform

仅执行变换类型操作。

备注

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

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

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

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

示例

为每个城市创建一个包含典型夏季温度的 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() 来对值进行平方。

>>> def square(x):
...     return x ** 2
>>> s.apply(square).execute()
London      400
New York    441
Helsinki    144
dtype: int64

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

>>> s.apply(lambda x: x ** 2).execute()
London      400
New York    441
Helsinki    144
dtype: int64

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

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

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

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

创建一个包含映射类型的 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(x):
...     if x is not None:
...         x["k2"] = 10
...     return x
>>> s.apply(custom_set_item, output_type="series", dtype=dict_(pa.string(), pa.int64())).execute()
1    [('k1', 1), ('k2', 10)]
2    [('k1', 3), ('k2', 10)]
3                       <NA>
dtype: map<string, int64>[pyarrow]