From 2be9e3e2badc054ab9a2c84778f4a775a234dc94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dea=20Mar=C3=ADa=20L=C3=A9on?= Date: Tue, 25 Jul 2023 16:10:34 +0200 Subject: [PATCH 1/2] User Guide correction on BaseIndexer --- doc/source/user_guide/window.rst | 47 ++++++++++++++------------------ 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/doc/source/user_guide/window.rst b/doc/source/user_guide/window.rst index 85e8762570736..9450eb804e5ff 100644 --- a/doc/source/user_guide/window.rst +++ b/doc/source/user_guide/window.rst @@ -259,33 +259,26 @@ For example, if we have the following :class:`DataFrame` and we want to use an expanding window where ``use_expanding`` is ``True`` otherwise a window of size 1, we can create the following ``BaseIndexer`` subclass: -.. code-block:: ipython - - In [2]: from pandas.api.indexers import BaseIndexer - - In [3]: 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 - - In [4]: indexer = CustomIndexer(window_size=1, use_expanding=use_expanding) - - In [5]: df.rolling(indexer).sum() - Out[5]: - values - 0 0.0 - 1 1.0 - 2 3.0 - 3 3.0 - 4 10.0 +.. ipython:: python + + from pandas.api.indexers import BaseIndexer + + class CustomIndexer(BaseIndexer): + def get_window_bounds(self, num_values, min_periods, center, closed, step): + 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 + + indexer = CustomIndexer(window_size=1, use_expanding=use_expanding) + + df.rolling(indexer).sum() You can view other examples of ``BaseIndexer`` subclasses `here `__ From 86c748823e69ff8481f30684e9413b66d67cea38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dea=20Mar=C3=ADa=20L=C3=A9on?= Date: Tue, 25 Jul 2023 17:55:46 +0200 Subject: [PATCH 2/2] Add to explanation of BaseIndex --- doc/source/user_guide/window.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/user_guide/window.rst b/doc/source/user_guide/window.rst index 9450eb804e5ff..d997aa119b359 100644 --- a/doc/source/user_guide/window.rst +++ b/doc/source/user_guide/window.rst @@ -244,7 +244,7 @@ a ``BaseIndexer`` subclass that allows a user to define a custom method for calc The ``BaseIndexer`` subclass will need to define a ``get_window_bounds`` method that returns a tuple of two arrays, the first being the starting indices of the windows and second being the ending indices of the windows. Additionally, ``num_values``, ``min_periods``, ``center``, ``closed`` -and will automatically be passed to ``get_window_bounds`` and the defined method must +and ``step`` will automatically be passed to ``get_window_bounds`` and the defined method must always accept these arguments. For example, if we have the following :class:`DataFrame`