简体   繁体   English

在 pandas 轧制中应用定制 window function

[英]Apply custom window function in pandas rolling

I have a DataFrame, eg我有一个 DataFrame,例如

df = pd.DataFrame([1,2,3,4,5,6,7,8,9])

Now I want to apply a rolling mean, eg现在我想应用滚动平均值,例如

df.rolling(window=3, win_type=None).mean()

which gives me a result with evenly weighted elements.这给了我一个均匀加权元素的结果。 Now I want to change the window function.现在我想更改 window function。 I know, that this is possible by passing a string (eg 'hann' ) to the win_type parameter.我知道,这可以通过将字符串(例如'hann' )传递给win_type参数来实现。

df.rolling(window=3, win_type='hann').mean()

Now the interesting point for me would be to apply a window function, that uses an exponentially decaying weighting, giving a high weight to the value "on the right" and lower weights to values "further to the left".现在对我来说有趣的一点是应用 window function,它使用指数衰减的权重,对“右侧”的值赋予较高的权重,而对“更左侧”的值赋予较低的权重。 This should be possible by using scipy.signal.windows.exponential and adjusting the parameters.这应该可以通过使用scipy.signal.windows.exponential并调整参数来实现。 However, I am struggling with passing those parameters as win_type only takes strings.但是,我正在努力传递这些参数,因为win_type只接受字符串。

When I try win_type='exponential' I get ValueError: exponential window requires tau .当我尝试win_type='exponential'我得到ValueError: exponential window requires tau

Can someone tell me how to pass parameters such as tau to win_type or even create a window function oneself?有人能告诉我如何将tau等参数传递给win_type甚至自己创建 window function 吗?

The answer is here答案在这里
Important.重要的。 Solution depends on pandas version.解决方案取决于 pandas 版本。
Python common Python 通用

import pandas as pd
df = pd.DataFrame([1,2,3,4,5,6,7,8,9])

In the case of tau =10.tau = 10的情况下。
For Pandas='0.24.2'对于熊猫='0.24.2'

df.rolling(window=(3,10), win_type='exponential').mean()

For Pandas='1.1.3' 对于熊猫='1.1.3'
 df.rolling(window=3, win_type='exponential').mean(tau=10)

Do not hesitate to add other version dependencies.毫不犹豫地添加其他版本依赖项。

Figured it out with an answer from the link provided by Stepan: Tau has to be passed in the function call, ie从 Stepan 提供的链接中找到答案: Tau 必须在 function 调用中传递,即

df.rolling(window=(3), win_type='exponential').mean(tau=10)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM