Reputation: 1883
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
Upvotes: 1
Views: 392
Reputation: 120509
You missed the step
parameter:
Replace:
def get_window_bounds(self, num_values, min_periods, center, closed):
With:
def get_window_bounds(self, num_values, min_periods, center, closed, step):
Better (typing and default values):
# https://github.com/pandas-dev/pandas/blob/8dab54d6573f7186ff0c3b6364d5e4dd635ff3e7/pandas/core/indexers/objects.py#L43-L71
def get_window_bounds(
self,
num_values: int = 0,
min_periods: int = None,
center: bool = None,
closed: str = None,
step: int = None,
) -> tuple[np.ndarray, np.ndarray]:
Upvotes: 2