From 30297855ac65b270cce112d627d46a8df9bb5782 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Tue, 3 Sep 2019 18:17:20 +0100 Subject: [PATCH 1/5] Make sure correct values are passed to Rolling._on when axis=1 --- doc/source/whatsnew/v1.0.0.rst | 2 +- pandas/core/window/rolling.py | 5 ++++- pandas/tests/window/test_rolling.py | 23 +++++++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index cd0714838a3f1..f6b497486c324 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -177,7 +177,7 @@ Groupby/resample/rolling ^^^^^^^^^^^^^^^^^^^^^^^^ - -- +- Bug in :meth:`DataFrame.rolling` not allowing for rolling over datetimes when ``axis=1`` (:issue: `28192`) - 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`) diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index a7e122fa3528f..08a8d66d980cf 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -1653,7 +1653,10 @@ def is_datetimelike(self): def _on(self): if self.on is None: - return self.obj.index + if self.axis == 0: + return self.obj.index + elif self.axis == 1: + return self.obj.transpose().index elif isinstance(self.obj, ABCDataFrame) and self.on in self.obj.columns: return Index(self.obj[self.on]) else: diff --git a/pandas/tests/window/test_rolling.py b/pandas/tests/window/test_rolling.py index b4787bf25e3bb..62aa222450d62 100644 --- a/pandas/tests/window/test_rolling.py +++ b/pandas/tests/window/test_rolling.py @@ -334,3 +334,26 @@ def test_readonly_array(self): result = pd.Series(arr).rolling(2).mean() expected = pd.Series([np.nan, 2, np.nan, np.nan, 4]) tm.assert_series_equal(result, expected) + + def test_rolling_datetime(self, axis_frame): + # GH-28192 + df = pd.DataFrame( + {i: [1] * 2 for i in pd.date_range("2019-8-01", "2019-08-03", freq="D")} + ) + if axis_frame in [0, "index"]: + result = df.T.rolling("2d", axis=axis_frame).sum().T + else: + result = df.rolling("2d", axis=axis_frame).sum() + expected = pd.DataFrame( + { + **{ + i: [1.0] * 2 + for i in pd.date_range("2019-8-01", periods=1, freq="D") + }, + **{ + i: [2.0] * 2 + for i in pd.date_range("2019-8-02", "2019-8-03", freq="D") + }, + } + ) + tm.assert_frame_equal(result, expected) From 7f56eb485de12b2c47b30cae955868106dd76152 Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Tue, 3 Sep 2019 18:50:39 +0100 Subject: [PATCH 2/5] Update rolling.py --- pandas/core/window/rolling.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 08a8d66d980cf..29ef2e917ae57 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -1656,7 +1656,7 @@ def _on(self): if self.axis == 0: return self.obj.index elif self.axis == 1: - return self.obj.transpose().index + return self.obj.columns elif isinstance(self.obj, ABCDataFrame) and self.on in self.obj.columns: return Index(self.obj[self.on]) else: From 4ac1cb651a9c32118332b8af6a93a72be608c825 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Wed, 4 Sep 2019 09:59:27 +0100 Subject: [PATCH 3/5] Capitalise 'd' as in documentation --- pandas/tests/window/test_rolling.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/window/test_rolling.py b/pandas/tests/window/test_rolling.py index 62aa222450d62..6d5956dd27bc9 100644 --- a/pandas/tests/window/test_rolling.py +++ b/pandas/tests/window/test_rolling.py @@ -341,9 +341,9 @@ def test_rolling_datetime(self, axis_frame): {i: [1] * 2 for i in pd.date_range("2019-8-01", "2019-08-03", freq="D")} ) if axis_frame in [0, "index"]: - result = df.T.rolling("2d", axis=axis_frame).sum().T + result = df.T.rolling("2D", axis=axis_frame).sum().T else: - result = df.rolling("2d", axis=axis_frame).sum() + result = df.rolling("2D", axis=axis_frame).sum() expected = pd.DataFrame( { **{ From 9611910d161b3489d91403acc81065168c52c1a6 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Wed, 4 Sep 2019 17:31:52 +0100 Subject: [PATCH 4/5] Parametrize over tz_naive_fixture --- pandas/tests/window/test_rolling.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pandas/tests/window/test_rolling.py b/pandas/tests/window/test_rolling.py index 6d5956dd27bc9..573ae53d060a2 100644 --- a/pandas/tests/window/test_rolling.py +++ b/pandas/tests/window/test_rolling.py @@ -335,10 +335,11 @@ def test_readonly_array(self): expected = pd.Series([np.nan, 2, np.nan, np.nan, 4]) tm.assert_series_equal(result, expected) - def test_rolling_datetime(self, axis_frame): + def test_rolling_datetime(self, axis_frame, tz_naive_fixture): # GH-28192 + tz = tz_naive_fixture df = pd.DataFrame( - {i: [1] * 2 for i in pd.date_range("2019-8-01", "2019-08-03", freq="D")} + {i: [1] * 2 for i in pd.date_range("2019-8-01", "2019-08-03", freq="D", tz=tz)} ) if axis_frame in [0, "index"]: result = df.T.rolling("2D", axis=axis_frame).sum().T @@ -348,11 +349,11 @@ def test_rolling_datetime(self, axis_frame): { **{ i: [1.0] * 2 - for i in pd.date_range("2019-8-01", periods=1, freq="D") + for i in pd.date_range("2019-8-01", periods=1, freq="D", tz=tz) }, **{ i: [2.0] * 2 - for i in pd.date_range("2019-8-02", "2019-8-03", freq="D") + for i in pd.date_range("2019-8-02", "2019-8-03", freq="D", tz=tz) }, } ) From f9cec774f6192f39ed0381f3f2824174ba803e7d Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Wed, 4 Sep 2019 17:32:37 +0100 Subject: [PATCH 5/5] autoformat --- pandas/tests/window/test_rolling.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pandas/tests/window/test_rolling.py b/pandas/tests/window/test_rolling.py index 573ae53d060a2..70ba85120af3c 100644 --- a/pandas/tests/window/test_rolling.py +++ b/pandas/tests/window/test_rolling.py @@ -339,7 +339,10 @@ def test_rolling_datetime(self, axis_frame, tz_naive_fixture): # GH-28192 tz = tz_naive_fixture df = pd.DataFrame( - {i: [1] * 2 for i in pd.date_range("2019-8-01", "2019-08-03", freq="D", tz=tz)} + { + i: [1] * 2 + for i in pd.date_range("2019-8-01", "2019-08-03", freq="D", tz=tz) + } ) if axis_frame in [0, "index"]: result = df.T.rolling("2D", axis=axis_frame).sum().T