Skip to content

Commit 474c1e2

Browse files
authored
DOC: User Guide correction on BaseIndexer (#54256)
* User Guide correction on BaseIndexer * Add to explanation of BaseIndex
1 parent b379f99 commit 474c1e2

File tree

1 file changed

+21
-28
lines changed

1 file changed

+21
-28
lines changed

doc/source/user_guide/window.rst

+21-28
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ a ``BaseIndexer`` subclass that allows a user to define a custom method for calc
244244
The ``BaseIndexer`` subclass will need to define a ``get_window_bounds`` method that returns
245245
a tuple of two arrays, the first being the starting indices of the windows and second being the
246246
ending indices of the windows. Additionally, ``num_values``, ``min_periods``, ``center``, ``closed``
247-
and will automatically be passed to ``get_window_bounds`` and the defined method must
247+
and ``step`` will automatically be passed to ``get_window_bounds`` and the defined method must
248248
always accept these arguments.
249249

250250
For example, if we have the following :class:`DataFrame`
@@ -259,33 +259,26 @@ For example, if we have the following :class:`DataFrame`
259259
and we want to use an expanding window where ``use_expanding`` is ``True`` otherwise a window of size
260260
1, we can create the following ``BaseIndexer`` subclass:
261261

262-
.. code-block:: ipython
263-
264-
In [2]: from pandas.api.indexers import BaseIndexer
265-
266-
In [3]: class CustomIndexer(BaseIndexer):
267-
...: def get_window_bounds(self, num_values, min_periods, center, closed):
268-
...: start = np.empty(num_values, dtype=np.int64)
269-
...: end = np.empty(num_values, dtype=np.int64)
270-
...: for i in range(num_values):
271-
...: if self.use_expanding[i]:
272-
...: start[i] = 0
273-
...: end[i] = i + 1
274-
...: else:
275-
...: start[i] = i
276-
...: end[i] = i + self.window_size
277-
...: return start, end
278-
279-
In [4]: indexer = CustomIndexer(window_size=1, use_expanding=use_expanding)
280-
281-
In [5]: df.rolling(indexer).sum()
282-
Out[5]:
283-
values
284-
0 0.0
285-
1 1.0
286-
2 3.0
287-
3 3.0
288-
4 10.0
262+
.. ipython:: python
263+
264+
from pandas.api.indexers import BaseIndexer
265+
266+
class CustomIndexer(BaseIndexer):
267+
def get_window_bounds(self, num_values, min_periods, center, closed, step):
268+
start = np.empty(num_values, dtype=np.int64)
269+
end = np.empty(num_values, dtype=np.int64)
270+
for i in range(num_values):
271+
if self.use_expanding[i]:
272+
start[i] = 0
273+
end[i] = i + 1
274+
else:
275+
start[i] = i
276+
end[i] = i + self.window_size
277+
return start, end
278+
279+
indexer = CustomIndexer(window_size=1, use_expanding=use_expanding)
280+
281+
df.rolling(indexer).sum()
289282
290283
You can view other examples of ``BaseIndexer`` subclasses `here <https://github.com/pandas-dev/pandas/blob/main/pandas/core/indexers/objects.py>`__
291284

0 commit comments

Comments
 (0)