-
-
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 all 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 |
---|---|---|
|
@@ -120,3 +120,53 @@ def get_window_bounds( | |
np.zeros(num_values, dtype=np.int64), | ||
np.arange(1, num_values + 1, dtype=np.int64), | ||
) | ||
|
||
|
||
class FixedForwardWindowIndexer(BaseIndexer): | ||
""" | ||
Creates window boundaries for fixed-length windows that include the | ||
current row. | ||
|
||
Examples | ||
-------- | ||
>>> df = pd.DataFrame({'B': [0, 1, 2, np.nan, 4]}) | ||
>>> df | ||
B | ||
0 0.0 | ||
1 1.0 | ||
2 2.0 | ||
3 NaN | ||
4 4.0 | ||
|
||
>>> indexer = pd.api.indexers.FixedForwardWindowIndexer(window_size=2) | ||
>>> df.rolling(window=indexer, min_periods=1).sum() | ||
B | ||
0 1.0 | ||
1 3.0 | ||
2 2.0 | ||
3 4.0 | ||
4 4.0 | ||
""" | ||
|
||
@Appender(get_window_bounds_doc) | ||
def get_window_bounds( | ||
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]: | ||
|
||
if center: | ||
raise ValueError("Forward-looking windows can't have center=True") | ||
if closed is not None: | ||
raise ValueError( | ||
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. need a test that hits these 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. Added to the test. |
||
"Forward-looking windows don't support setting the closed argument" | ||
) | ||
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 As far as I've been able to test, the arguments are being set by the |
||
|
||
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 |
Uh oh!
There was an error while loading. Please reload this page.