Trying to run the code below for Python (3.9.13) Pandas (1.5.2) docs but running to the error below. Any suggestions!
ValueError: CustomWindow does not implement the correct signature for get_window_bounds
# Libraries
import numpy as np
import pandas as pd
from pandas.api.indexers import BaseIndexer
# Data
df = pd.DataFrame({"values": range(5)})
# Customization
class CustomIndexer(BaseIndexer):
def get_window_bounds(self, num_values, min_periods, center, closed):
start = np.empty(num_values, dtype=np.int64)
end = np.empty(num_values, dtype=np.int64)
for i in range(num_values):
if self.use_expanding[i]:
start[i] = 0
end[i] = i + 1
else:
start[i] = i
end[i] = i + self.window_size
return start, end
# Instantiate class
use_expanding = [True, False, True, False, True]
indexer = CustomIndexer(window_size=2, use_expanding=use_expanding)
# Perform rolling
df.rolling(indexer).sum()
Expected output
values
0 0.0
1 1.0
2 3.0
3 3.0
4 10.0
You missed the
stepparameter:Replace:
With:
Better (typing and default values):