Perhaps looking at this quick example will help you to understand what I try to do:
import pandas as pd
df = pd.DataFrame({"A": [10,20,30,50,70,40], "B": [20,30,10,15,20,30]})
def _custom_function(X):
# whatever... just for the purpose of the example
# but I need X to be the actual df and not a series
Y = sum((X['A'] / X['B']) + (0.2 * X['B']))
return Y
df['C'] = df.rolling(2).apply(_custom_function, axis=0)
When the custom function is called, X is Series type and only the first columns of the df. Is it possible to pass a df trought the apply function ?
Edit: it is possible to use rolling().apply():
import pandas as pd
df = pd.DataFrame({"A": [10,20,30,50,70,40], "B": [20,30,10,15,20,30]})
def _custom_function(X):
# whatever... just for the purpose of the example
Y = sum(0.2 * X)
return Y
df['C'] = df['A'].rolling(2).apply(_custom_function)
Second edit: list comprehension with rolling does not behave as expected
for x in df.rolling(3):
print(x)
As you can see in the example below both approaches don't give the same output:
import pandas as pd
df = pd.DataFrame({"A": [10,20,30,50,70,40], "B": [20,30,10,15,20,30]})
df['C'] = 0.2
def _custom_function_df(X):
# whatever... just for the purpose of the example
# but I need X to be the actual df and not a series
Y = sum(X['C'] * X['B'])
return Y
def _custom_function_series(X):
# whatever... just for the purpose of the example
# but I need X to be the actual df and not a series
Y = sum(0.2 * X)
return Y
df['result'] = df['B'].rolling(3).apply(_custom_function_series)
df['result2'] = [x.pipe(_custom_function_df) for x in df.rolling(3, min_periods=3)]
The list comprehension with rolling output the first lines (no expected NaN), but starts the correct rolling ONLY after len(x) = 3, the rolling window.
Thanks in advance !
