diff --git a/doc/source/user_guide/window.rst b/doc/source/user_guide/window.rst index 08641bc5b17ae..afb706d61f405 100644 --- a/doc/source/user_guide/window.rst +++ b/doc/source/user_guide/window.rst @@ -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"] 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 +~~~~~~~~~~~~~~~~~~~~~~ + +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] + df1 + +Or we can also do it using FixedForwardWindowIndexer which basically Creates window boundaries +for fixed-length windows that include the current row. + +.. 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 + + indexer = pd.api.indexers.FixedForwardWindowIndexer(window_size=2) + + df2 = df.rolling(window=indexer, min_periods=1).sum() + 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 ` subclass implements a closed fixed-width forward-looking rolling window, and we can use it as follows: -.. ipython:: ipython +.. 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 - 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