Skip to content

Commit e35916d

Browse files
authored
DOC: Fixing EX01 - Added examples (pandas-dev#54254)
* examples BaseIndexer & VariableOffsetWindowIndexer * Corrected BaseIndexer for loop * Removed doctest skip
1 parent edc0870 commit e35916d

File tree

2 files changed

+58
-10
lines changed

2 files changed

+58
-10
lines changed

ci/code_checks.sh

-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
8181
pandas.util.hash_array \
8282
pandas.util.hash_pandas_object \
8383
pandas_object \
84-
pandas.api.indexers.BaseIndexer \
85-
pandas.api.indexers.VariableOffsetWindowIndexer \
8684
pandas.api.extensions.ExtensionDtype \
8785
pandas.api.extensions.ExtensionArray \
8886
pandas.arrays.NumpyExtensionArray \

pandas/core/indexers/objects.py

+58-8
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,34 @@
4141

4242

4343
class BaseIndexer:
44-
"""Base class for window bounds calculations."""
44+
"""
45+
Base class for window bounds calculations.
46+
47+
Examples
48+
--------
49+
>>> from pandas.api.indexers import BaseIndexer
50+
>>> class CustomIndexer(BaseIndexer):
51+
... def get_window_bounds(self, num_values, min_periods, center, closed, step):
52+
... start = np.empty(num_values, dtype=np.int64)
53+
... end = np.empty(num_values, dtype=np.int64)
54+
... for i in range(num_values):
55+
... start[i] = i
56+
... end[i] = i + self.window_size
57+
... return start, end
58+
>>> df = pd.DataFrame({"values": range(5)})
59+
>>> indexer = CustomIndexer(window_size=2)
60+
>>> df.rolling(indexer).sum()
61+
values
62+
0 1.0
63+
1 3.0
64+
2 5.0
65+
3 7.0
66+
4 4.0
67+
"""
4568

4669
def __init__(
4770
self, index_array: np.ndarray | None = None, window_size: int = 0, **kwargs
4871
) -> None:
49-
"""
50-
Parameters
51-
----------
52-
**kwargs :
53-
keyword arguments that will be available when get_window_bounds is called
54-
"""
5572
self.index_array = index_array
5673
self.window_size = window_size
5774
# Set user defined kwargs as attributes that can be used in get_window_bounds
@@ -127,7 +144,40 @@ def get_window_bounds(
127144

128145

129146
class VariableOffsetWindowIndexer(BaseIndexer):
130-
"""Calculate window boundaries based on a non-fixed offset such as a BusinessDay."""
147+
"""
148+
Calculate window boundaries based on a non-fixed offset such as a BusinessDay.
149+
150+
Examples
151+
--------
152+
>>> from pandas.api.indexers import VariableOffsetWindowIndexer
153+
>>> df = pd.DataFrame(range(10), index=pd.date_range("2020", periods=10))
154+
>>> offset = pd.offsets.BDay(1)
155+
>>> indexer = VariableOffsetWindowIndexer(index=df.index, offset=offset)
156+
>>> df
157+
0
158+
2020-01-01 0
159+
2020-01-02 1
160+
2020-01-03 2
161+
2020-01-04 3
162+
2020-01-05 4
163+
2020-01-06 5
164+
2020-01-07 6
165+
2020-01-08 7
166+
2020-01-09 8
167+
2020-01-10 9
168+
>>> df.rolling(indexer).sum()
169+
0
170+
2020-01-01 0.0
171+
2020-01-02 1.0
172+
2020-01-03 2.0
173+
2020-01-04 3.0
174+
2020-01-05 7.0
175+
2020-01-06 12.0
176+
2020-01-07 6.0
177+
2020-01-08 7.0
178+
2020-01-09 8.0
179+
2020-01-10 9.0
180+
"""
131181

132182
def __init__(
133183
self,

0 commit comments

Comments
 (0)