From 0fbb6c36d36cdcb386ecb75318d6eb9dc305116a Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Thu, 25 Jun 2020 10:18:46 -0700 Subject: [PATCH 1/4] DOC: Add example of NonFixedVariableWindowIndexer usage --- doc/source/user_guide/computation.rst | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/doc/source/user_guide/computation.rst b/doc/source/user_guide/computation.rst index 897e5d5fb0e24..33bb6d64e7b6b 100644 --- a/doc/source/user_guide/computation.rst +++ b/doc/source/user_guide/computation.rst @@ -594,6 +594,32 @@ and we want to use an expanding window where ``use_expanding`` is ``True`` other 4 10.0 You can view other examples of ``BaseIndexer`` subclasses `here `__ +One subclass of note within those examples is the ``NonFixedVariableWindowIndexer`` that allows +rolling operations over a non-fixed offset like a ``BusinessDay``. + +.. code-block:: ipython + + In [1]: from pandas.core.window.indexers import NonFixedVariableWindowIndexer + + In [2]: df = pd.DataFrame(range(10), index=pd.date_range('2020', periods=10)) + + In [3]: offset = pd.offsets.BDay(1) + + In [4]: indexer = NonFixedVariableWindowIndexer(index=df.index, offset=offset) + + In [5]: df.rolling(indexer).sum() + Out[5]: + 0 + 2020-01-01 0.0 + 2020-01-02 1.0 + 2020-01-03 2.0 + 2020-01-04 3.0 + 2020-01-05 7.0 + 2020-01-06 12.0 + 2020-01-07 6.0 + 2020-01-08 7.0 + 2020-01-09 8.0 + 2020-01-10 9.0 .. versionadded:: 1.1 From 170da457482ae942cc62821d2525d9a3bf282153 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Thu, 25 Jun 2020 19:33:48 -0700 Subject: [PATCH 2/4] move version added --- doc/source/user_guide/computation.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/doc/source/user_guide/computation.rst b/doc/source/user_guide/computation.rst index 33bb6d64e7b6b..76a123f8e5b8f 100644 --- a/doc/source/user_guide/computation.rst +++ b/doc/source/user_guide/computation.rst @@ -594,6 +594,9 @@ and we want to use an expanding window where ``use_expanding`` is ``True`` other 4 10.0 You can view other examples of ``BaseIndexer`` subclasses `here `__ + +.. versionadded:: 1.1 + One subclass of note within those examples is the ``NonFixedVariableWindowIndexer`` that allows rolling operations over a non-fixed offset like a ``BusinessDay``. @@ -621,8 +624,6 @@ rolling operations over a non-fixed offset like a ``BusinessDay``. 2020-01-09 8.0 2020-01-10 9.0 -.. versionadded:: 1.1 - For some problems knowledge of the future is available for analysis. For example, this occurs when each data point is a full time series read from an experiment, and the task is to extract underlying conditions. In these cases it can be useful to perform forward-looking rolling window computations. From 95eaec5e342f28103576e0af78f0c1b85b2613ba Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Thu, 25 Jun 2020 19:37:58 -0700 Subject: [PATCH 3/4] use ipython block --- doc/source/user_guide/computation.rst | 29 +++++++-------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/doc/source/user_guide/computation.rst b/doc/source/user_guide/computation.rst index 76a123f8e5b8f..487607da3c5de 100644 --- a/doc/source/user_guide/computation.rst +++ b/doc/source/user_guide/computation.rst @@ -600,29 +600,14 @@ You can view other examples of ``BaseIndexer`` subclasses `here Date: Sat, 27 Jun 2020 15:42:41 -0700 Subject: [PATCH 4/4] Add NonFixedVariableWindowIndexer to api.indexers --- doc/source/user_guide/computation.rst | 2 +- doc/source/whatsnew/v1.1.0.rst | 1 + pandas/api/indexers/__init__.py | 13 +++++++++++-- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/doc/source/user_guide/computation.rst b/doc/source/user_guide/computation.rst index 487607da3c5de..3a524996ea6d9 100644 --- a/doc/source/user_guide/computation.rst +++ b/doc/source/user_guide/computation.rst @@ -602,7 +602,7 @@ rolling operations over a non-fixed offset like a ``BusinessDay``. .. ipython:: python - from pandas.core.window.indexers import NonFixedVariableWindowIndexer + from pandas.api.indexers import NonFixedVariableWindowIndexer df = pd.DataFrame(range(10), index=pd.date_range('2020', periods=10)) offset = pd.offsets.BDay(1) indexer = NonFixedVariableWindowIndexer(index=df.index, offset=offset) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index c5eb2febe8ae9..8a9cf75e34213 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -673,6 +673,7 @@ Other API changes - ``loc`` lookups with an object-dtype :class:`Index` and an integer key will now raise ``KeyError`` instead of ``TypeError`` when key is missing (:issue:`31905`) - Using a :func:`pandas.api.indexers.BaseIndexer` with ``count``, ``min``, ``max``, ``median``, ``skew``, ``cov``, ``corr`` will now return correct results for any monotonic :func:`pandas.api.indexers.BaseIndexer` descendant (:issue:`32865`) - Added a :func:`pandas.api.indexers.FixedForwardWindowIndexer` class to support forward-looking windows during ``rolling`` operations. +- Added a :func:`pandas.api.indexers.NonFixedVariableWindowIndexer` class to support ``rolling`` operations with non-fixed offsets (:issue:`34994`) - Added :class:`pandas.errors.InvalidIndexError` (:issue:`34570`). - :meth:`DataFrame.swaplevels` now raises a ``TypeError`` if the axis is not a :class:`MultiIndex`. Previously an ``AttributeError`` was raised (:issue:`31126`) diff --git a/pandas/api/indexers/__init__.py b/pandas/api/indexers/__init__.py index 0b36b53675e23..89a9d8bfafdf1 100644 --- a/pandas/api/indexers/__init__.py +++ b/pandas/api/indexers/__init__.py @@ -3,6 +3,15 @@ """ from pandas.core.indexers import check_array_indexer -from pandas.core.window.indexers import BaseIndexer, FixedForwardWindowIndexer +from pandas.core.window.indexers import ( + BaseIndexer, + FixedForwardWindowIndexer, + NonFixedVariableWindowIndexer, +) -__all__ = ["check_array_indexer", "BaseIndexer", "FixedForwardWindowIndexer"] +__all__ = [ + "check_array_indexer", + "BaseIndexer", + "FixedForwardWindowIndexer", + "NonFixedVariableWindowIndexer", +]