Skip to content

DOC: Fixing EX01 - Added examples #54254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
pandas.util.hash_array \
pandas.util.hash_pandas_object \
pandas_object \
pandas.api.indexers.BaseIndexer \
pandas.api.indexers.VariableOffsetWindowIndexer \
pandas.api.extensions.ExtensionDtype \
pandas.api.extensions.ExtensionArray \
pandas.arrays.NumpyExtensionArray \
Expand Down
66 changes: 58 additions & 8 deletions pandas/core/indexers/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,34 @@


class BaseIndexer:
"""Base class for window bounds calculations."""
"""
Base class for window bounds calculations.

Examples
--------
>>> 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):
... start[i] = i
... end[i] = i + self.window_size
... return start, end
>>> df = pd.DataFrame({"values": range(5)})
>>> indexer = CustomIndexer(window_size=2)
>>> df.rolling(indexer).sum()
values
0 1.0
1 3.0
2 5.0
3 7.0
4 4.0
"""

def __init__(
self, index_array: np.ndarray | None = None, window_size: int = 0, **kwargs
) -> None:
"""
Parameters
----------
**kwargs :
keyword arguments that will be available when get_window_bounds is called
"""
self.index_array = index_array
self.window_size = window_size
# Set user defined kwargs as attributes that can be used in get_window_bounds
Expand Down Expand Up @@ -127,7 +144,40 @@ def get_window_bounds(


class VariableOffsetWindowIndexer(BaseIndexer):
"""Calculate window boundaries based on a non-fixed offset such as a BusinessDay."""
"""
Calculate window boundaries based on a non-fixed offset such as a BusinessDay.

Examples
--------
>>> from pandas.api.indexers import VariableOffsetWindowIndexer
>>> df = pd.DataFrame(range(10), index=pd.date_range("2020", periods=10))
>>> offset = pd.offsets.BDay(1)
>>> indexer = VariableOffsetWindowIndexer(index=df.index, offset=offset)
>>> df
0
2020-01-01 0
2020-01-02 1
2020-01-03 2
2020-01-04 3
2020-01-05 4
2020-01-06 5
2020-01-07 6
2020-01-08 7
2020-01-09 8
2020-01-10 9
>>> df.rolling(indexer).sum()
0
2020-01-01 0.0
2020-01-02 1.0
2020-01-03 2.0
2020-01-04 3.0
2020-01-05 7.0
2020-01-06 12.0
2020-01-07 6.0
2020-01-08 7.0
2020-01-09 8.0
2020-01-10 9.0
"""

def __init__(
self,
Expand Down