Skip to content

Commit c15c0bb

Browse files
committed
DOC: Add suggested changes to reverse rolling window code examples (pandas-dev#38627).
Move code example as suggested in pull request (pandas-dev#39091): pandas-dev#39091 Compiled document to check it looked fine by running: python make.py --single user_guide/window.rst
1 parent 243cc9f commit c15c0bb

File tree

1 file changed

+20
-44
lines changed

1 file changed

+20
-44
lines changed

doc/source/user_guide/window.rst

Lines changed: 20 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -201,42 +201,6 @@ from present information back to past information. This allows the rolling windo
201201
202202
df
203203
204-
.. _window.reverse_rolling_window:
205-
206-
Reverse rolling window
207-
~~~~~~~~~~~~~~~~~~~~~~
208-
209-
Get the window of a rolling function to look forward.
210-
211-
We can achieve this by using slicing in python by applying rolling aggregation and then flipping the result
212-
as shown in example below:
213-
214-
.. ipython:: python
215-
216-
df = pd.DataFrame(
217-
data=[
218-
[pd.Timestamp("2018-01-01 00:00:00"), 100],
219-
[pd.Timestamp("2018-01-01 00:00:01"), 101],
220-
[pd.Timestamp("2018-01-01 00:00:03"), 103],
221-
[pd.Timestamp("2018-01-01 00:00:04"), 111],
222-
],
223-
columns=["time", "value"],
224-
).set_index("time")
225-
df
226-
227-
reversed_df = df[::-1].rolling("2s").sum()[::-1]
228-
reversed_df
229-
230-
Or we can also do it using :meth:`api.indexers.FixedForwardWindowIndexer` which basically creates window boundaries
231-
for fixed-length windows that include the current row.
232-
233-
.. ipython:: python
234-
235-
indexer = pd.api.indexers.FixedForwardWindowIndexer(window_size=2)
236-
237-
reversed_df = df.rolling(window=indexer, min_periods=1).sum()
238-
reversed_df
239-
240204
241205
.. _window.custom_rolling_window:
242206

@@ -312,12 +276,16 @@ rolling operations over a non-fixed offset like a ``BusinessDay``.
312276
df
313277
df.rolling(indexer).sum()
314278
315-
For some problems knowledge of the future is available for analysis. For example, this occurs when
316-
each data point is a full time series read from an experiment, and the task is to extract underlying
317-
conditions. In these cases it can be useful to perform forward-looking rolling window computations.
318-
:func:`FixedForwardWindowIndexer <pandas.api.indexers.FixedForwardWindowIndexer>` class is available for this purpose.
319-
This :func:`BaseIndexer <pandas.api.indexers.BaseIndexer>` subclass implements a closed fixed-width
320-
forward-looking rolling window, and we can use it as follows:
279+
280+
.. _window.reverse_rolling_window:
281+
282+
Reverse rolling window
283+
~~~~~~~~~~~~~~~~~~~~~~
284+
285+
Get the window of a rolling function to look forward.
286+
287+
We can achieve this by using slicing in python by applying rolling aggregation and then flipping the result
288+
as shown in example below:
321289

322290
.. ipython:: python
323291
@@ -332,10 +300,18 @@ forward-looking rolling window, and we can use it as follows:
332300
).set_index("time")
333301
df
334302
303+
reversed_df = df[::-1].rolling("2s").sum()[::-1]
304+
reversed_df
305+
306+
Or we can also do it using :meth:`api.indexers.FixedForwardWindowIndexer` which basically creates window boundaries
307+
for fixed-length windows that include the current row.
308+
309+
.. ipython:: python
310+
335311
indexer = pd.api.indexers.FixedForwardWindowIndexer(window_size=2)
336312
337-
df_out = df.rolling(window=indexer, min_periods=1).sum()
338-
df_out
313+
reversed_df = df.rolling(window=indexer, min_periods=1).sum()
314+
reversed_df
339315
340316
341317
.. _window.rolling_apply:

0 commit comments

Comments
 (0)