Skip to content

Commit d265e21

Browse files
authored
DOC: reverse rolling window (#38627) (#41842)
* DOC: Add examples to do reversed rolling window (#38627). File is copied from @vangorade last changes in pull request (#39091): #39091 Fixed typo to pass pre-commit codespell hook. * DOC: Add some suggested changes. From pull request (#39091): #39091 - remove noqa - change line back to single quotes - rename df1 to reversed_df * DOC: Add reference to FixedForwardWindowIndexer. * DOC: Add more suggested changes. From pull request (#39091): #39091 - remove repeated df initialization in code example - rename df2 to reversed_df * DOC: Add suggested changes to reverse rolling window code examples (#38627). Move code example as suggested in pull request (#39091): #39091 Compiled document to check it looked fine by running: python make.py --single user_guide/window.rst * DOC: Revert more double quotes to single quotes. Unecessary changes. * DOC: Change binary window description to match master branch. Description was not modified after fetching and merging, so pasted from master branch: https://github.com/pandas-dev/pandas/blob/master/doc/source/user_guide/window.rst * DOC: Change exponentially weighted mean formula to one in master branch. Since fetching and merging didn't modify this line. * DOC: Replace window.rst with file from pandas-dev master branch. To only add reverse rolling window section and avoid any unwanted changes. * DOC: Add section with reverse rolling window example. * DOC: Add section markup. Forgot to add. * DOC: Delete the same code example. FixedForwaredWindowIndexer example is same as the one above the added section. * DOC: Remove section and first sentence. Modify sentence slightly to show this example should output same result. * DOC: Remove unecessary words. Coding is already in Python.
1 parent da12db8 commit d265e21

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

doc/source/user_guide/window.rst

+18
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,24 @@ forward-looking rolling window, and we can use it as follows:
299299
indexer = FixedForwardWindowIndexer(window_size=2)
300300
df.rolling(indexer, min_periods=1).sum()
301301
302+
We can also achieve this by using slicing, applying rolling aggregation, and then flipping the result as shown in example below:
303+
304+
.. ipython:: python
305+
306+
df = pd.DataFrame(
307+
data=[
308+
[pd.Timestamp("2018-01-01 00:00:00"), 100],
309+
[pd.Timestamp("2018-01-01 00:00:01"), 101],
310+
[pd.Timestamp("2018-01-01 00:00:03"), 103],
311+
[pd.Timestamp("2018-01-01 00:00:04"), 111],
312+
],
313+
columns=["time", "value"],
314+
).set_index("time")
315+
df
316+
317+
reversed_df = df[::-1].rolling("2s").sum()[::-1]
318+
reversed_df
319+
302320
.. _window.rolling_apply:
303321

304322
Rolling apply

0 commit comments

Comments
 (0)