From ce0a5e93036dc2232e636bf40265038547469a73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C5=A0koda?= Date: Wed, 4 Sep 2019 12:53:27 +0200 Subject: [PATCH 01/12] fix Rolling for multi-index and reversed index. --- doc/source/whatsnew/v0.25.2.rst | 4 ++-- pandas/core/window/rolling.py | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/doc/source/whatsnew/v0.25.2.rst b/doc/source/whatsnew/v0.25.2.rst index 1cdf213d81a74..d5339c39de06e 100644 --- a/doc/source/whatsnew/v0.25.2.rst +++ b/doc/source/whatsnew/v0.25.2.rst @@ -78,8 +78,8 @@ Groupby/resample/rolling ^^^^^^^^^^^^^^^^^^^^^^^^ - Bug incorrectly raising an ``IndexError`` when passing a list of quantiles to :meth:`pandas.core.groupby.DataFrameGroupBy.quantile` (:issue:`28113`). -- -- +- Allow usage of ``rolling`` ``on`` argument with multi-index column. +- Allow usage of ``rolling`` with monotonic decreasing time indexes. - Reshaping diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 29ef2e917ae57..b84bdab5067b6 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -1651,18 +1651,19 @@ def is_datetimelike(self): @cache_readonly def _on(self): - if self.on is None: if self.axis == 0: return self.obj.index elif self.axis == 1: return self.obj.columns + elif isinstance(self.on, Index): + return self.on elif isinstance(self.obj, ABCDataFrame) and self.on in self.obj.columns: return Index(self.obj[self.on]) else: raise ValueError( "invalid on specified as {0}, " - "must be a column (if DataFrame) " + "must be a column (of DataFrame), an Index " "or None".format(self.on) ) @@ -1706,9 +1707,9 @@ def validate(self): def _validate_monotonic(self): """ - Validate on is_monotonic. + Validate monotonic (increasing or decreasing). """ - if not self._on.is_monotonic: + if not self._on.is_monotonic_increasing and self._on.is_monotonic_decreasing: formatted = self.on or "index" raise ValueError("{0} must be monotonic".format(formatted)) From 1e1af78bc8d98eb547cfb13a0a0ec90f809e3506 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C5=A0koda?= Date: Thu, 5 Sep 2019 17:16:36 +0200 Subject: [PATCH 02/12] fix whatsnew --- doc/source/whatsnew/v0.25.2.rst | 4 ++-- doc/source/whatsnew/v1.0.0.rst | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v0.25.2.rst b/doc/source/whatsnew/v0.25.2.rst index d5339c39de06e..1cdf213d81a74 100644 --- a/doc/source/whatsnew/v0.25.2.rst +++ b/doc/source/whatsnew/v0.25.2.rst @@ -78,8 +78,8 @@ Groupby/resample/rolling ^^^^^^^^^^^^^^^^^^^^^^^^ - Bug incorrectly raising an ``IndexError`` when passing a list of quantiles to :meth:`pandas.core.groupby.DataFrameGroupBy.quantile` (:issue:`28113`). -- Allow usage of ``rolling`` ``on`` argument with multi-index column. -- Allow usage of ``rolling`` with monotonic decreasing time indexes. +- +- - Reshaping diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 58892b316c940..e6d7cb917863f 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -176,8 +176,9 @@ Plotting Groupby/resample/rolling ^^^^^^^^^^^^^^^^^^^^^^^^ -- - Bug in :meth:`DataFrame.rolling` not allowing for rolling over datetimes when ``axis=1`` (:issue: `28192`) +- Bug in :meth:`DataFrame.rolling` not allowing rolling over multi-index levels. +- Bug in :meth:`DataFrame.rolling` not allowing rolling on monotonic decreasing time indexes. - Bug in :meth:`DataFrame.groupby` not offering selection by column name when ``axis=1`` (:issue:`27614`) - Bug in :meth:`DataFrameGroupby.agg` not able to use lambda function with named aggregation (:issue:`27519`) From de83db4c6ac413b6b78488a2f3677c82eacbb483 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C5=A0koda?= Date: Fri, 6 Sep 2019 10:38:41 +0200 Subject: [PATCH 03/12] fix rolling on multi-index level --- pandas/core/window/rolling.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index b84bdab5067b6..f742efad29a46 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -126,7 +126,7 @@ def _create_blocks(self): obj = self._selected_obj # filter out the on from the object - if self.on is not None: + if self.on is not None and not isinstance(self.on, Index): if obj.ndim == 2: obj = obj.reindex(columns=obj.columns.difference([self.on]), copy=False) blocks = obj._to_dict_of_blocks(copy=False).values() @@ -1709,8 +1709,10 @@ def _validate_monotonic(self): """ Validate monotonic (increasing or decreasing). """ - if not self._on.is_monotonic_increasing and self._on.is_monotonic_decreasing: - formatted = self.on or "index" + if not self._on.is_monotonic_increasing and not self._on.is_monotonic_decreasing: + formatted = self.on + if self.on is None: + formatted = "index" raise ValueError("{0} must be monotonic".format(formatted)) def _validate_freq(self): From f78cf28046fd3fc69c237315e2abac0a118c87d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C5=A0koda?= Date: Fri, 6 Sep 2019 10:38:49 +0200 Subject: [PATCH 04/12] add tests --- pandas/tests/window/test_rolling.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/pandas/tests/window/test_rolling.py b/pandas/tests/window/test_rolling.py index 70ba85120af3c..8d1826cd8645b 100644 --- a/pandas/tests/window/test_rolling.py +++ b/pandas/tests/window/test_rolling.py @@ -361,3 +361,30 @@ def test_rolling_datetime(self, axis_frame, tz_naive_fixture): } ) tm.assert_frame_equal(result, expected) + + def test_rolling_decreasing(self): + index = [ + pd.Timestamp('20190101 09:00:00'), + pd.Timestamp('20190101 09:00:02'), + pd.Timestamp('20190101 09:00:03'), + pd.Timestamp('20190101 09:00:05'), + pd.Timestamp('20190101 09:00:06') + ] + + df = pd.DataFrame({'column': [3, 4, 4, 2, 1]}, index=reversed(index)) + result = df.rolling('2s').min() + tm.assert_frame_equal( + result, + pd.DataFrame({'column': [3., 3., 3., 2., 1.]}, index=reversed(index)) + ) + + def test_rolling_multi_index(self): + df = pd.DataFrame( + {'column': range(6)}, + index=pd.MultiIndex.from_product([pd.date_range('20190101', periods=3), range(2)], names=['date', 'seq']) + ) + result = df.rolling('10d', on=df.index.get_level_values('date')).sum() + tm.assert_frame_equal( + result, + pd.DataFrame({'column': [0., 1., 3., 6., 10., 15.]}, index = df.index) + ) From 859bfea288b1242363c498470b375a3055aa2ac1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C5=A0koda?= Date: Fri, 6 Sep 2019 10:38:58 +0200 Subject: [PATCH 05/12] add issues into whatsnew --- doc/source/whatsnew/v1.0.0.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index e6d7cb917863f..a9cb32a80af9f 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -177,8 +177,8 @@ Groupby/resample/rolling ^^^^^^^^^^^^^^^^^^^^^^^^ - Bug in :meth:`DataFrame.rolling` not allowing for rolling over datetimes when ``axis=1`` (:issue: `28192`) -- Bug in :meth:`DataFrame.rolling` not allowing rolling over multi-index levels. -- Bug in :meth:`DataFrame.rolling` not allowing rolling on monotonic decreasing time indexes. +- Bug in :meth:`DataFrame.rolling` not allowing rolling over multi-index levels (:issue: `15584`). +- Bug in :meth:`DataFrame.rolling` not allowing rolling on monotonic decreasing time indexes (:issue: `19248`). - Bug in :meth:`DataFrame.groupby` not offering selection by column name when ``axis=1`` (:issue:`27614`) - Bug in :meth:`DataFrameGroupby.agg` not able to use lambda function with named aggregation (:issue:`27519`) From 908342d4adcc1020e663315fcef712012b9acb3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C5=A0koda?= Date: Fri, 6 Sep 2019 10:40:53 +0200 Subject: [PATCH 06/12] pep8 corrections --- pandas/core/window/rolling.py | 5 ++++- pandas/tests/window/test_rolling.py | 7 +++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index f742efad29a46..ce379e238fd6e 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -1709,7 +1709,10 @@ def _validate_monotonic(self): """ Validate monotonic (increasing or decreasing). """ - if not self._on.is_monotonic_increasing and not self._on.is_monotonic_decreasing: + if ( + not self._on.is_monotonic_increasing + and not self._on.is_monotonic_decreasing + ): formatted = self.on if self.on is None: formatted = "index" diff --git a/pandas/tests/window/test_rolling.py b/pandas/tests/window/test_rolling.py index 8d1826cd8645b..02bce7c8d010a 100644 --- a/pandas/tests/window/test_rolling.py +++ b/pandas/tests/window/test_rolling.py @@ -381,10 +381,13 @@ def test_rolling_decreasing(self): def test_rolling_multi_index(self): df = pd.DataFrame( {'column': range(6)}, - index=pd.MultiIndex.from_product([pd.date_range('20190101', periods=3), range(2)], names=['date', 'seq']) + index=pd.MultiIndex.from_product( + [pd.date_range('20190101', periods=3), range(2)], + names=['date', 'seq'] + ) ) result = df.rolling('10d', on=df.index.get_level_values('date')).sum() tm.assert_frame_equal( result, - pd.DataFrame({'column': [0., 1., 3., 6., 10., 15.]}, index = df.index) + pd.DataFrame({'column': [0., 1., 3., 6., 10., 15.]}, index=df.index) ) From bc77e09da189fb9284edcaca405a9cbc986babd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C5=A0koda?= Date: Fri, 6 Sep 2019 11:33:50 +0200 Subject: [PATCH 07/12] fix test --- pandas/tests/window/test_timeseries_window.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/tests/window/test_timeseries_window.py b/pandas/tests/window/test_timeseries_window.py index e057eadae9da8..23a7f4b450b8e 100644 --- a/pandas/tests/window/test_timeseries_window.py +++ b/pandas/tests/window/test_timeseries_window.py @@ -106,7 +106,9 @@ def test_monotonic_on(self): df.rolling("2s").sum() # non-monotonic - df.index = reversed(df.index.tolist()) + non_monotonic_index = df.index.to_list() + non_monotonic_index[0] = non_monotonic_index[3] + df.index = non_monotonic_index assert not df.index.is_monotonic with pytest.raises(ValueError): From 33c3ccaeffb3028848d7e2e87a1f15c06806e842 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C5=A0koda?= Date: Fri, 6 Sep 2019 12:31:02 +0200 Subject: [PATCH 08/12] fix black --- pandas/tests/window/test_rolling.py | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/pandas/tests/window/test_rolling.py b/pandas/tests/window/test_rolling.py index 02bce7c8d010a..658b55d38fb3b 100644 --- a/pandas/tests/window/test_rolling.py +++ b/pandas/tests/window/test_rolling.py @@ -364,30 +364,29 @@ def test_rolling_datetime(self, axis_frame, tz_naive_fixture): def test_rolling_decreasing(self): index = [ - pd.Timestamp('20190101 09:00:00'), - pd.Timestamp('20190101 09:00:02'), - pd.Timestamp('20190101 09:00:03'), - pd.Timestamp('20190101 09:00:05'), - pd.Timestamp('20190101 09:00:06') + pd.Timestamp("20190101 09:00:00"), + pd.Timestamp("20190101 09:00:02"), + pd.Timestamp("20190101 09:00:03"), + pd.Timestamp("20190101 09:00:05"), + pd.Timestamp("20190101 09:00:06"), ] - df = pd.DataFrame({'column': [3, 4, 4, 2, 1]}, index=reversed(index)) - result = df.rolling('2s').min() + df = pd.DataFrame({"column": [3, 4, 4, 2, 1]}, index=reversed(index)) + result = df.rolling("2s").min() tm.assert_frame_equal( result, - pd.DataFrame({'column': [3., 3., 3., 2., 1.]}, index=reversed(index)) + pd.DataFrame({"column": [3.0, 3.0, 3.0, 2.0, 1.0]}, index=reversed(index)), ) def test_rolling_multi_index(self): df = pd.DataFrame( - {'column': range(6)}, + {"column": range(6)}, index=pd.MultiIndex.from_product( - [pd.date_range('20190101', periods=3), range(2)], - names=['date', 'seq'] - ) + [pd.date_range("20190101", periods=3), range(2)], names=["date", "seq"] + ), ) - result = df.rolling('10d', on=df.index.get_level_values('date')).sum() + result = df.rolling("10d", on=df.index.get_level_values("date")).sum() tm.assert_frame_equal( result, - pd.DataFrame({'column': [0., 1., 3., 6., 10., 15.]}, index=df.index) + pd.DataFrame({"column": [0.0, 1.0, 3.0, 6.0, 10.0, 15.0]}, index=df.index), ) From c5a062f7f2a39ef1fb1d916965286ba96b0dc8e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C5=A0koda?= Date: Sat, 7 Sep 2019 22:08:11 +0200 Subject: [PATCH 09/12] review fixes --- pandas/core/window/rolling.py | 5 +-- pandas/tests/window/test_rolling.py | 29 -------------- pandas/tests/window/test_timeseries_window.py | 38 ++++++++++++++++++- 3 files changed, 38 insertions(+), 34 deletions(-) diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index ce379e238fd6e..74040b31a9c05 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -1709,10 +1709,7 @@ def _validate_monotonic(self): """ Validate monotonic (increasing or decreasing). """ - if ( - not self._on.is_monotonic_increasing - and not self._on.is_monotonic_decreasing - ): + if not (self._on.is_monotonic_increasing or self._on.is_monotonic_decreasing): formatted = self.on if self.on is None: formatted = "index" diff --git a/pandas/tests/window/test_rolling.py b/pandas/tests/window/test_rolling.py index 658b55d38fb3b..70ba85120af3c 100644 --- a/pandas/tests/window/test_rolling.py +++ b/pandas/tests/window/test_rolling.py @@ -361,32 +361,3 @@ def test_rolling_datetime(self, axis_frame, tz_naive_fixture): } ) tm.assert_frame_equal(result, expected) - - def test_rolling_decreasing(self): - index = [ - pd.Timestamp("20190101 09:00:00"), - pd.Timestamp("20190101 09:00:02"), - pd.Timestamp("20190101 09:00:03"), - pd.Timestamp("20190101 09:00:05"), - pd.Timestamp("20190101 09:00:06"), - ] - - df = pd.DataFrame({"column": [3, 4, 4, 2, 1]}, index=reversed(index)) - result = df.rolling("2s").min() - tm.assert_frame_equal( - result, - pd.DataFrame({"column": [3.0, 3.0, 3.0, 2.0, 1.0]}, index=reversed(index)), - ) - - def test_rolling_multi_index(self): - df = pd.DataFrame( - {"column": range(6)}, - index=pd.MultiIndex.from_product( - [pd.date_range("20190101", periods=3), range(2)], names=["date", "seq"] - ), - ) - result = df.rolling("10d", on=df.index.get_level_values("date")).sum() - tm.assert_frame_equal( - result, - pd.DataFrame({"column": [0.0, 1.0, 3.0, 6.0, 10.0, 15.0]}, index=df.index), - ) diff --git a/pandas/tests/window/test_timeseries_window.py b/pandas/tests/window/test_timeseries_window.py index 23a7f4b450b8e..cccac40e17e47 100644 --- a/pandas/tests/window/test_timeseries_window.py +++ b/pandas/tests/window/test_timeseries_window.py @@ -105,10 +105,15 @@ def test_monotonic_on(self): assert df.index.is_monotonic df.rolling("2s").sum() - # non-monotonic + def test_non_monotonic_on(self): + df = DataFrame( + {"A": date_range("20130101", periods=5, freq="s"), "B": range(5)} + ) + df.set_index("A", inplace=True) non_monotonic_index = df.index.to_list() non_monotonic_index[0] = non_monotonic_index[3] df.index = non_monotonic_index + assert not df.index.is_monotonic with pytest.raises(ValueError): @@ -692,3 +697,34 @@ def test_rolling_cov_offset(self): expected2 = ss.rolling(3, min_periods=1).cov() tm.assert_series_equal(result, expected2) + + def test_rolling_on_decreasing_index(self): + # GH-19248 + index = [ + pd.Timestamp("20190101 09:00:00"), + pd.Timestamp("20190101 09:00:02"), + pd.Timestamp("20190101 09:00:03"), + pd.Timestamp("20190101 09:00:05"), + pd.Timestamp("20190101 09:00:06"), + ] + + df = pd.DataFrame({"column": [3, 4, 4, 2, 1]}, index=reversed(index)) + result = df.rolling("2s").min() + expected = ( + pd.DataFrame({"column": [3.0, 3.0, 3.0, 2.0, 1.0]}, index=reversed(index)), + ) + tm.assert_frame_equal(result, expected) + + def test_rolling_on_multi_index_level(self): + # GH-15584 + df = pd.DataFrame( + {"column": range(6)}, + index=pd.MultiIndex.from_product( + [pd.date_range("20190101", periods=3), range(2)], names=["date", "seq"] + ), + ) + result = df.rolling("10d", on=df.index.get_level_values("date")).sum() + expected = ( + pd.DataFrame({"column": [0.0, 1.0, 3.0, 6.0, 10.0, 15.0]}, index=df.index), + ) + tm.assert_frame_equal(result, expected) From 1ec868b0cdc1eff4d0ebb8eac5b2d886846330e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C5=A0koda?= Date: Sat, 7 Sep 2019 23:21:46 +0200 Subject: [PATCH 10/12] fix tests --- pandas/tests/window/test_timeseries_window.py | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/pandas/tests/window/test_timeseries_window.py b/pandas/tests/window/test_timeseries_window.py index cccac40e17e47..fb55264c789e7 100644 --- a/pandas/tests/window/test_timeseries_window.py +++ b/pandas/tests/window/test_timeseries_window.py @@ -701,30 +701,30 @@ def test_rolling_cov_offset(self): def test_rolling_on_decreasing_index(self): # GH-19248 index = [ - pd.Timestamp("20190101 09:00:00"), - pd.Timestamp("20190101 09:00:02"), - pd.Timestamp("20190101 09:00:03"), - pd.Timestamp("20190101 09:00:05"), - pd.Timestamp("20190101 09:00:06"), + Timestamp("20190101 09:00:00"), + Timestamp("20190101 09:00:02"), + Timestamp("20190101 09:00:03"), + Timestamp("20190101 09:00:05"), + Timestamp("20190101 09:00:06"), ] - df = pd.DataFrame({"column": [3, 4, 4, 2, 1]}, index=reversed(index)) + df = DataFrame({"column": [3, 4, 4, 2, 1]}, index=reversed(index)) result = df.rolling("2s").min() expected = ( - pd.DataFrame({"column": [3.0, 3.0, 3.0, 2.0, 1.0]}, index=reversed(index)), + DataFrame({"column": [3.0, 3.0, 3.0, 2.0, 1.0]}, index=reversed(index)), ) tm.assert_frame_equal(result, expected) def test_rolling_on_multi_index_level(self): # GH-15584 - df = pd.DataFrame( + df = DataFrame( {"column": range(6)}, - index=pd.MultiIndex.from_product( - [pd.date_range("20190101", periods=3), range(2)], names=["date", "seq"] + index=MultiIndex.from_product( + [date_range("20190101", periods=3), range(2)], names=["date", "seq"] ), ) result = df.rolling("10d", on=df.index.get_level_values("date")).sum() expected = ( - pd.DataFrame({"column": [0.0, 1.0, 3.0, 6.0, 10.0, 15.0]}, index=df.index), + DataFrame({"column": [0.0, 1.0, 3.0, 6.0, 10.0, 15.0]}, index=df.index), ) tm.assert_frame_equal(result, expected) From 753d01c426c1ccdd87712df6722d445f83144dc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C5=A0koda?= Date: Fri, 20 Sep 2019 20:54:25 +0200 Subject: [PATCH 11/12] fix tests #2 --- pandas/tests/window/test_timeseries_window.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/pandas/tests/window/test_timeseries_window.py b/pandas/tests/window/test_timeseries_window.py index fb55264c789e7..9cc15c2a47563 100644 --- a/pandas/tests/window/test_timeseries_window.py +++ b/pandas/tests/window/test_timeseries_window.py @@ -1,7 +1,15 @@ import numpy as np import pytest -from pandas import DataFrame, Index, Series, Timestamp, date_range, to_datetime +from pandas import ( + DataFrame, + Index, + MultiIndex, + Series, + Timestamp, + date_range, + to_datetime, +) import pandas.util.testing as tm import pandas.tseries.offsets as offsets @@ -710,8 +718,8 @@ def test_rolling_on_decreasing_index(self): df = DataFrame({"column": [3, 4, 4, 2, 1]}, index=reversed(index)) result = df.rolling("2s").min() - expected = ( - DataFrame({"column": [3.0, 3.0, 3.0, 2.0, 1.0]}, index=reversed(index)), + expected = DataFrame( + {"column": [3.0, 3.0, 3.0, 2.0, 1.0]}, index=reversed(index) ) tm.assert_frame_equal(result, expected) @@ -724,7 +732,7 @@ def test_rolling_on_multi_index_level(self): ), ) result = df.rolling("10d", on=df.index.get_level_values("date")).sum() - expected = ( - DataFrame({"column": [0.0, 1.0, 3.0, 6.0, 10.0, 15.0]}, index=df.index), + expected = DataFrame( + {"column": [0.0, 1.0, 3.0, 6.0, 10.0, 15.0]}, index=df.index ) tm.assert_frame_equal(result, expected) From 1b46dc504c715ecc3ea7d81240eb0a8bb4e0c441 Mon Sep 17 00:00:00 2001 From: Jan Skoda Date: Sat, 19 Oct 2019 20:31:43 +0200 Subject: [PATCH 12/12] review fixes --- pandas/core/window/rolling.py | 10 +++++----- pandas/tests/window/test_timeseries_window.py | 3 ++- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 74040b31a9c05..3e75340ed3bcf 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -70,7 +70,7 @@ def __init__( center: Optional[bool] = False, win_type: Optional[str] = None, axis: Axis = 0, - on: Optional[str] = None, + on: Optional[Union[str, Index]] = None, closed: Optional[str] = None, **kwargs ): @@ -637,10 +637,10 @@ class Window(_Window): Provide a window type. If ``None``, all points are evenly weighted. See the notes below for further information. on : str, optional - For a DataFrame, a datetime-like column on which to calculate the rolling - window, rather than the DataFrame's index. Provided integer column is - ignored and excluded from result since an integer index is not used to - calculate the rolling window. + For a DataFrame, a datetime-like column or MultiIndex level on which + to calculate the rolling window, rather than the DataFrame's index. + Provided integer column is ignored and excluded from result since + an integer index is not used to calculate the rolling window. axis : int or str, default 0 closed : str, default None Make the interval closed on the 'right', 'left', 'both' or diff --git a/pandas/tests/window/test_timeseries_window.py b/pandas/tests/window/test_timeseries_window.py index 9cc15c2a47563..7055e5b538bea 100644 --- a/pandas/tests/window/test_timeseries_window.py +++ b/pandas/tests/window/test_timeseries_window.py @@ -114,10 +114,11 @@ def test_monotonic_on(self): df.rolling("2s").sum() def test_non_monotonic_on(self): + # GH 19248 df = DataFrame( {"A": date_range("20130101", periods=5, freq="s"), "B": range(5)} ) - df.set_index("A", inplace=True) + df = df.set_index("A") non_monotonic_index = df.index.to_list() non_monotonic_index[0] = non_monotonic_index[3] df.index = non_monotonic_index