0

I have a DataFrame, e.g.

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

Now I want to apply a rolling mean, e.g.

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. I know, that this is possible by passing a string (e.g. 'hann') to the win_type parameter.

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". This should be possible by using scipy.signal.windows.exponential and adjusting the parameters. However, I am struggling with passing those parameters as win_type only takes strings.

When I try win_type='exponential' I get 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?

2 Answers 2

1

The answer is here
Important. Solution depends on pandas version.
Python common

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

In the case of tau=10.
For Pandas='0.24.2'

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

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

Do not hesitate to add other version dependencies.

4
  • Thanks a lot. However, copying this exact code still produces ``` ValueError: exponential window requires tau ```
    – Messor
    Jan 10, 2021 at 11:51
  • Weird. These 3 rows of code work for me. Which versions of pandas and numpy are you using?
    – Stepan
    Jan 10, 2021 at 11:58
  • My pandas is 1.1.3
    – Messor
    Jan 10, 2021 at 12:04
  • 1
    I used pandas='0.24.2', and after checking with version 1.1.3 I can claim that the solution is version dependent.
    – Stepan
    Jan 10, 2021 at 12:54
0

Figured it out with an answer from the link provided by Stepan: Tau has to be passed in the function call, i.e.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.