From 78529184587cd1cdaa6e1898244880eae337b023 Mon Sep 17 00:00:00 2001 From: ganevgv Date: Thu, 21 Nov 2019 01:04:20 +0000 Subject: [PATCH 1/5] add test for rolling max with DatetimeIndex --- pandas/tests/window/test_moments.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pandas/tests/window/test_moments.py b/pandas/tests/window/test_moments.py index 6e4bc621d7f49..b3aef73b81335 100644 --- a/pandas/tests/window/test_moments.py +++ b/pandas/tests/window/test_moments.py @@ -1891,6 +1891,20 @@ def test_rolling_corr_with_zero_variance(self, window): assert s.rolling(window=window).corr(other=other).isna().all() + def test_rolling_max_datetimeindex(self): + # GH 21096 + n = 10 + index = pd.date_range(start="2018-1-1 01:00:00", freq="1min", periods=n) + s = Series(data=0, index=index) + + s.iloc[1] = np.nan + s.iloc[-1] = 2 + maxes = s.rolling(window=f"{n}min").max() + result = maxes.value_counts(dropna=False) + expected = Series(data={0.0: n - 1, 2.0: 1}) + + tm.assert_series_equal(result, expected) + def _check_pairwise_moment(self, dispatch, name, **kwargs): def get_result(obj, obj2=None): return getattr(getattr(obj, dispatch)(**kwargs), name)(obj2) From 041df3fdc97a3a23f418d095e1c2bb30e6150b5e Mon Sep 17 00:00:00 2001 From: ganevgv Date: Sat, 23 Nov 2019 00:29:54 +0000 Subject: [PATCH 2/5] move and simplify test --- pandas/tests/window/test_moments.py | 14 -------------- pandas/tests/window/test_timeseries_window.py | 12 ++++++++++++ 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/pandas/tests/window/test_moments.py b/pandas/tests/window/test_moments.py index b3aef73b81335..6e4bc621d7f49 100644 --- a/pandas/tests/window/test_moments.py +++ b/pandas/tests/window/test_moments.py @@ -1891,20 +1891,6 @@ def test_rolling_corr_with_zero_variance(self, window): assert s.rolling(window=window).corr(other=other).isna().all() - def test_rolling_max_datetimeindex(self): - # GH 21096 - n = 10 - index = pd.date_range(start="2018-1-1 01:00:00", freq="1min", periods=n) - s = Series(data=0, index=index) - - s.iloc[1] = np.nan - s.iloc[-1] = 2 - maxes = s.rolling(window=f"{n}min").max() - result = maxes.value_counts(dropna=False) - expected = Series(data={0.0: n - 1, 2.0: 1}) - - tm.assert_series_equal(result, expected) - def _check_pairwise_moment(self, dispatch, name, **kwargs): def get_result(obj, obj2=None): return getattr(getattr(obj, dispatch)(**kwargs), name)(obj2) diff --git a/pandas/tests/window/test_timeseries_window.py b/pandas/tests/window/test_timeseries_window.py index 7055e5b538bea..02969a6c6e822 100644 --- a/pandas/tests/window/test_timeseries_window.py +++ b/pandas/tests/window/test_timeseries_window.py @@ -535,6 +535,18 @@ def test_ragged_max(self): expected["B"] = [0.0, 1, 2, 3, 4] tm.assert_frame_equal(result, expected) + def test_minutes_freq_max(self): + # GH 21096 + n = 10 + index = date_range(start="2018-1-1 01:00:00", freq="1min", periods=n) + s = Series(data=0, index=index) + s.iloc[1] = np.nan + s.iloc[-1] = 2 + result = s.rolling(window=f"{n}min").max() + expected = Series(data=[0] * (n - 1) + [2.0], index=index) + + tm.assert_series_equal(result, expected) + def test_ragged_apply(self, raw): df = self.ragged From a4729b2b5099c9061c997ebcc00fb4ff692879ef Mon Sep 17 00:00:00 2001 From: ganevgv Date: Fri, 29 Nov 2019 20:49:17 +0000 Subject: [PATCH 3/5] parametrize over freqs and ops --- pandas/tests/window/test_timeseries_window.py | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/pandas/tests/window/test_timeseries_window.py b/pandas/tests/window/test_timeseries_window.py index 02969a6c6e822..cad1e88f8a181 100644 --- a/pandas/tests/window/test_timeseries_window.py +++ b/pandas/tests/window/test_timeseries_window.py @@ -535,15 +535,36 @@ def test_ragged_max(self): expected["B"] = [0.0, 1, 2, 3, 4] tm.assert_frame_equal(result, expected) - def test_minutes_freq_max(self): + @pytest.mark.parametrize( + "freq, op, result_data", + [ + ("ms", "min", [0.0] * 10), + ("ms", "mean", [0.0] * 9 + [2.0 / 9]), + ("ms", "max", [0.0] * 9 + [2.0]), + ("s", "min", [0.0] * 10), + ("s", "mean", [0.0] * 9 + [2.0 / 9]), + ("s", "max", [0.0] * 9 + [2.0]), + ("min", "min", [0.0] * 10), + ("min", "mean", [0.0] * 9 + [2.0 / 9]), + ("min", "max", [0.0] * 9 + [2.0]), + ("h", "min", [0.0] * 10), + ("h", "mean", [0.0] * 9 + [2.0 / 9]), + ("h", "max", [0.0] * 9 + [2.0]), + ("D", "min", [0.0] * 10), + ("D", "mean", [0.0] * 9 + [2.0 / 9]), + ("D", "max", [0.0] * 9 + [2.0]), + ], + ) + def test_freqs_ops(self, freq, op, result_data): # GH 21096 - n = 10 - index = date_range(start="2018-1-1 01:00:00", freq="1min", periods=n) + index = date_range( + start="2018-1-1 01:00:00", freq="1{}".format(freq), periods=10 + ) s = Series(data=0, index=index) s.iloc[1] = np.nan s.iloc[-1] = 2 - result = s.rolling(window=f"{n}min").max() - expected = Series(data=[0] * (n - 1) + [2.0], index=index) + result = getattr(s.rolling(window="10{}".format(freq)), op)() + expected = Series(data=result_data, index=index) tm.assert_series_equal(result, expected) From eb58e54d20031ae8a7380da13a4f82ddcc608072 Mon Sep 17 00:00:00 2001 From: ganevgv Date: Sun, 1 Dec 2019 03:36:29 +0000 Subject: [PATCH 4/5] use f-string --- pandas/tests/window/test_timeseries_window.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/window/test_timeseries_window.py b/pandas/tests/window/test_timeseries_window.py index cad1e88f8a181..fc804dab3b58e 100644 --- a/pandas/tests/window/test_timeseries_window.py +++ b/pandas/tests/window/test_timeseries_window.py @@ -558,12 +558,12 @@ def test_ragged_max(self): def test_freqs_ops(self, freq, op, result_data): # GH 21096 index = date_range( - start="2018-1-1 01:00:00", freq="1{}".format(freq), periods=10 + start="2018-1-1 01:00:00", freq=f"1{freq}", periods=10 ) s = Series(data=0, index=index) s.iloc[1] = np.nan s.iloc[-1] = 2 - result = getattr(s.rolling(window="10{}".format(freq)), op)() + result = getattr(s.rolling(window=f"10{freq}"), op)() expected = Series(data=result_data, index=index) tm.assert_series_equal(result, expected) From f13216f19502c21539bdea3d9702c03aa159a544 Mon Sep 17 00:00:00 2001 From: ganevgv Date: Sun, 1 Dec 2019 13:33:01 +0000 Subject: [PATCH 5/5] passes black --- pandas/tests/window/test_timeseries_window.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pandas/tests/window/test_timeseries_window.py b/pandas/tests/window/test_timeseries_window.py index fc804dab3b58e..46582b4b50c84 100644 --- a/pandas/tests/window/test_timeseries_window.py +++ b/pandas/tests/window/test_timeseries_window.py @@ -557,9 +557,7 @@ def test_ragged_max(self): ) def test_freqs_ops(self, freq, op, result_data): # GH 21096 - index = date_range( - start="2018-1-1 01:00:00", freq=f"1{freq}", periods=10 - ) + index = date_range(start="2018-1-1 01:00:00", freq=f"1{freq}", periods=10) s = Series(data=0, index=index) s.iloc[1] = np.nan s.iloc[-1] = 2