-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
DOC: Added reverse rolling window examples #39091
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
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 |
---|---|---|
|
@@ -50,16 +50,16 @@ As noted above, some operations support specifying a window based on a time offs | |
|
||
.. ipython:: python | ||
|
||
s = pd.Series(range(5), index=pd.date_range('2020-01-01', periods=5, freq='1D')) | ||
s.rolling(window='2D').sum() | ||
s = pd.Series(range(5), index=pd.date_range("2020-01-01", periods=5, freq="1D")) | ||
s.rolling(window="2D").sum() | ||
|
||
Additionally, some methods support chaining a ``groupby`` operation with a windowing operation | ||
which will first group the data by the specified keys and then perform a windowing operation per group. | ||
|
||
.. ipython:: python | ||
|
||
df = pd.DataFrame({'A': ['a', 'b', 'a', 'b', 'a'], 'B': range(5)}) | ||
df.groupby('A').expanding().sum() | ||
df = pd.DataFrame({"A": ["a", "b", "a", "b", "a"], "B": range(5)}) | ||
df.groupby("A").expanding().sum() | ||
|
||
.. note:: | ||
|
||
|
@@ -95,8 +95,11 @@ be calculated with :meth:`~Rolling.apply` by specifying a separate column of wei | |
arr[:, :2] = (x[:, :2] * x[:, 2]).sum(axis=0) / x[:, 2].sum() | ||
return arr | ||
|
||
|
||
df = pd.DataFrame([[1, 2, 0.6], [2, 3, 0.4], [3, 4, 0.2], [4, 5, 0.7]]) | ||
df.rolling(2, method="table", min_periods=0).apply(weighted_mean, raw=True, engine="numba") # noqa:E501 | ||
df.rolling(2, method="table", min_periods=0).apply( | ||
weighted_mean, raw=True, engine="numba" | ||
) # noqa:E501 | ||
|
||
|
||
All windowing operations support a ``min_periods`` argument that dictates the minimum amount of | ||
|
@@ -132,13 +135,13 @@ time based index must be monotonic. | |
|
||
.. ipython:: python | ||
|
||
times = ['2020-01-01', '2020-01-03', '2020-01-04', '2020-01-05', '2020-01-29'] | ||
times = ["2020-01-01", "2020-01-03", "2020-01-04", "2020-01-05", "2020-01-29"] | ||
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. prefer that you don't change the quoting as its non-essential to this change |
||
s = pd.Series(range(5), index=pd.DatetimeIndex(times)) | ||
s | ||
# Window with 2 observations | ||
s.rolling(window=2).sum() | ||
# Window with 2 days worth of observations | ||
s.rolling(window='2D').sum() | ||
s.rolling(window="2D").sum() | ||
|
||
For all supported aggregation functions, see :ref:`api.functions_rolling`. | ||
|
||
|
@@ -198,6 +201,53 @@ from present information back to past information. This allows the rolling windo | |
|
||
df | ||
|
||
.. _window.reverse_rolling_window: | ||
|
||
Reverse rolling window | ||
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 just add this as an example of using the 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. Sorry for late reply. Sure i will add it. |
||
~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
Get the window of a rolling function to look forward. | ||
|
||
We can achieve this by using slicing in python by applying rolling aggregation and then flipping the result | ||
as shown in example below: | ||
|
||
.. ipython:: python | ||
|
||
df = pd.DataFrame( | ||
data=[ | ||
[pd.Timestamp("2018-01-01 00:00:00"), 100], | ||
[pd.Timestamp("2018-01-01 00:00:01"), 101], | ||
[pd.Timestamp("2018-01-01 00:00:03"), 103], | ||
[pd.Timestamp("2018-01-01 00:00:04"), 111], | ||
], | ||
columns=["time", "value"], | ||
).set_index("time") | ||
df | ||
|
||
df1 = df[::-1].rolling("2s").sum()[::-1] | ||
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. call this |
||
df1 | ||
|
||
Or we can also do it using FixedForwardWindowIndexer which basically Creates window boundaries | ||
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 the refernce to this, something like |
||
for fixed-length windows that include the current row. | ||
|
||
.. ipython:: python | ||
|
||
df = pd.DataFrame( | ||
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. no need to repeat this |
||
data=[ | ||
[pd.Timestamp("2018-01-01 00:00:00"), 100], | ||
[pd.Timestamp("2018-01-01 00:00:01"), 101], | ||
[pd.Timestamp("2018-01-01 00:00:03"), 103], | ||
[pd.Timestamp("2018-01-01 00:00:04"), 111], | ||
], | ||
columns=["time", "value"], | ||
).set_index("time") | ||
df | ||
|
||
indexer = pd.api.indexers.FixedForwardWindowIndexer(window_size=2) | ||
|
||
df2 = df.rolling(window=indexer, min_periods=1).sum() | ||
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. name as above |
||
df2 | ||
|
||
|
||
.. _window.custom_rolling_window: | ||
|
||
|
@@ -280,11 +330,23 @@ conditions. In these cases it can be useful to perform forward-looking rolling w | |
This :func:`BaseIndexer <pandas.api.indexers.BaseIndexer>` subclass implements a closed fixed-width | ||
forward-looking rolling window, and we can use it as follows: | ||
|
||
.. ipython:: ipython | ||
.. ipython:: python | ||
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. instead of having this here I woul dmove the example from above here. no reason to have this in 2 places and this is the best one. |
||
|
||
df = pd.DataFrame( | ||
data=[ | ||
[pd.Timestamp("2018-01-01 00:00:00"), 100], | ||
[pd.Timestamp("2018-01-01 00:00:01"), 101], | ||
[pd.Timestamp("2018-01-01 00:00:03"), 103], | ||
[pd.Timestamp("2018-01-01 00:00:04"), 111], | ||
], | ||
columns=["time", "value"], | ||
).set_index("time") | ||
df | ||
|
||
from pandas.api.indexers import FixedForwardWindowIndexer | ||
indexer = FixedForwardWindowIndexer(window_size=2) | ||
df.rolling(indexer, min_periods=1).sum() | ||
indexer = pd.api.indexers.FixedForwardWindowIndexer(window_size=2) | ||
|
||
df_out = df.rolling(window=indexer, min_periods=1).sum() | ||
df_out | ||
|
||
|
||
.. _window.rolling_apply: | ||
|
@@ -302,6 +364,7 @@ the windows are cast as :class:`Series` objects (``raw=False``) or ndarray objec | |
def mad(x): | ||
return np.fabs(x - x.mean()).mean() | ||
|
||
|
||
s = pd.Series(range(10)) | ||
s.rolling(window=4).apply(mad, raw=True) | ||
|
||
|
@@ -407,11 +470,7 @@ can even be omitted: | |
|
||
.. ipython:: python | ||
|
||
covs = ( | ||
df[["B", "C", "D"]] | ||
.rolling(window=4) | ||
.cov(df[["A", "B", "C"]], pairwise=True) | ||
) | ||
covs = df[["B", "C", "D"]].rolling(window=4).cov(df[["A", "B", "C"]], pairwise=True) | ||
covs | ||
|
||
|
||
|
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.
this noqa is no longer needed