-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: provide standard BaseIndexers in pandas.api.indexers #33236
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
Changes from 14 commits
31fd213
d2b7743
5afb066
212448f
0aa7980
fff110c
c94cbd5
4e5aa02
271cc36
ac8fd5f
dc7fec8
e1a6f73
dadcb3d
46f45c2
7464c92
6cdd20d
fb43381
9702839
728ada2
ffd50c5
3ed0437
1e3efdd
916bd6a
9bc5fc3
bd265f4
d08081b
6e7c629
81c4216
1451340
768197a
bc59bb7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -571,6 +571,29 @@ and we want to use an expanding window where ``use_expanding`` is ``True`` other | |
3 3.0 | ||
4 10.0 | ||
|
||
.. versionadded:: 1.1 | ||
|
||
For some problems knowledge of the future is available for analysis. For example, this occurs when | ||
each data point is a full time series read from an experiment, and the task is to extract underlying | ||
conditions. In these cases it can be useful to perform forward-looking rolling window computations. | ||
``FixedForwardWindowIndexer`` class is available for this purpose. This ``BaseIndexer`` subclass | ||
implements a closed fixed-width forward-looking rolling window, and we can use it as follows: | ||
|
||
.. code-block:: ipython | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use an ipython block here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jreback There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we don't use code-blocks on executable code. pls use an ipython block so that the doc-build executes this, that way it will never get out of date. |
||
|
||
In [5]: from pandas.api.indexers import FixedForwardWindowIndexer | ||
|
||
In [6]: indexer = FixedForwardWindowIndexer(window_size=2) | ||
|
||
In [7]: df.rolling(indexer, min_periods=1).sum() | ||
Out[8]: | ||
values | ||
0 1.0 | ||
1 3.0 | ||
2 5.0 | ||
3 7.0 | ||
4 4.0 | ||
|
||
|
||
.. _stats.rolling_window.endpoints: | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,3 +120,23 @@ def get_window_bounds( | |
np.zeros(num_values, dtype=np.int64), | ||
np.arange(1, num_values + 1, dtype=np.int64), | ||
) | ||
|
||
|
||
class FixedForwardWindowIndexer(BaseIndexer): | ||
"""Calculate fixed forward-looking rolling window bounds,""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add Examples section here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
||
@Appender(get_window_bounds_doc) | ||
def get_window_bounds( | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self, | ||
num_values: int = 0, | ||
min_periods: Optional[int] = None, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should raise if min_periods, center, closed are not None There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Implemented it a bit differently, pinged you in the comment on the implementation. |
||
center: Optional[bool] = None, | ||
closed: Optional[str] = None, | ||
) -> Tuple[np.ndarray, np.ndarray]: | ||
|
||
start = np.arange(num_values, dtype="int64") | ||
end_s = start[: -self.window_size] + self.window_size | ||
end_e = np.full(self.window_size, num_values, dtype="int64") | ||
end = np.concatenate([end_s, end_e]) | ||
|
||
return start, end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can use the ref here, e.g. the one you are using in the whatsnew
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jreback I've included a ref in whatsnew, pointing to this subsection of the user guide. If you meant something different, please tell me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no I mean a ref on FixedForwardIndexer here that points to the api (makes a clickable link), and one for BaseIndexer