将参数传递给rolling\u apply

2024-10-01 22:35:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要向pd.rolling_apply的目标函数传递几个参数

我的目标函数是:

  def complexFunction(i,j,k,l,verbosity=False):
    ...
    return 0.0

这些论点是位置性的吗?什么是规范的方法?在


Tags: 方法函数规范false目标参数returndef
3条回答

根据您链接的documentation,您可以使用args关键字传递参数,第一个参数将由rolling_apply传入,您可以将其余参数定义为元组并将其传递到args关键字参数中。示例-

 pd.rolling_apply(df,<window>,complexFunction,args=(j,k,l))

示例/演示-

^{pr2}$

下面是从functools使用partial的方法。在

from functools import partial

def complex_function(data, x, y):
    # some calculations
    return sum(data) * x * y

my_partial_func = partial(complex_function, x=2, y=5)

ser = pd.Series(np.arange(10))
pd.rolling_apply(ser, window=5, func=my_partial_func)

0    NaN
1    NaN
2    NaN
3    NaN
4    100
5    150
6    200
7    250
8    300
9    350
dtype: float64    

我通常让我的函数以一行作为它的单一输入

  def complexFunction(row,verbosity=False):
    i = row.i
    j = row.j
    k = row.k

    return 0.0

  pd.rolling_apply(df, complexFunction)

相关问题 更多 >

    热门问题