From de9b8f8a6cf25cc47f1b0486c3f9e88741b23984 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Mon, 16 Nov 2020 18:12:30 -0800 Subject: [PATCH 1/5] REF: window tests --- pandas/tests/window/conftest.py | 2 +- pandas/tests/window/test_rolling.py | 1 - pandas/tests/window/test_timeseries_window.py | 19 ++----------------- .../{test_window.py => test_win_type.py} | 0 4 files changed, 3 insertions(+), 19 deletions(-) rename pandas/tests/window/{test_window.py => test_win_type.py} (100%) diff --git a/pandas/tests/window/conftest.py b/pandas/tests/window/conftest.py index 1780925202593..0c37214757d2b 100644 --- a/pandas/tests/window/conftest.py +++ b/pandas/tests/window/conftest.py @@ -35,7 +35,7 @@ def win_types_special(request): @pytest.fixture( - params=["sum", "mean", "median", "max", "min", "var", "std", "kurt", "skew"] + params=["sum", "mean", "median", "max", "min", "var", "std", "kurt", "skew", "count"] ) def arithmetic_win_operators(request): return request.param diff --git a/pandas/tests/window/test_rolling.py b/pandas/tests/window/test_rolling.py index 6cad93f2d77ba..75e1a771b70ea 100644 --- a/pandas/tests/window/test_rolling.py +++ b/pandas/tests/window/test_rolling.py @@ -122,7 +122,6 @@ def test_numpy_compat(method): getattr(r, method)(dtype=np.float64) -@pytest.mark.parametrize("closed", ["left", "right", "both", "neither"]) def test_closed_fixed(closed, arithmetic_win_operators): # GH 34315 func_name = arithmetic_win_operators diff --git a/pandas/tests/window/test_timeseries_window.py b/pandas/tests/window/test_timeseries_window.py index d9fcb538c97c1..0782ef2f4ce7b 100644 --- a/pandas/tests/window/test_timeseries_window.py +++ b/pandas/tests/window/test_timeseries_window.py @@ -621,23 +621,8 @@ def test_all(self, f): expected = er.quantile(0.5) tm.assert_frame_equal(result, expected) - @pytest.mark.parametrize( - "f", - [ - "sum", - "mean", - "count", - "median", - "std", - "var", - "kurt", - "skew", - "min", - "max", - ], - ) - def test_all2(self, f): - + def test_all2(self, arithmetic_win_operators): + f = arithmetic_win_operators # more sophisticated comparison of integer vs. # time-based windowing df = DataFrame( diff --git a/pandas/tests/window/test_window.py b/pandas/tests/window/test_win_type.py similarity index 100% rename from pandas/tests/window/test_window.py rename to pandas/tests/window/test_win_type.py From d8db4c0295de3d1eeeda71150220ae2ec1575276 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Tue, 17 Nov 2020 09:47:12 -0800 Subject: [PATCH 2/5] Unwravel an expanding test --- .../test_moments_consistency_expanding.py | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/pandas/tests/window/moments/test_moments_consistency_expanding.py b/pandas/tests/window/moments/test_moments_consistency_expanding.py index eb348fda5782b..8c1fa968c1133 100644 --- a/pandas/tests/window/moments/test_moments_consistency_expanding.py +++ b/pandas/tests/window/moments/test_moments_consistency_expanding.py @@ -111,6 +111,25 @@ def test_expanding_corr_pairwise(frame): tm.assert_frame_equal(result, rolling_result) +@pytest.mark.parametrize( + "func,static_comp", + [("sum", np.sum), ("mean", np.mean), ("max", np.max), ("min", np.min)], + ids=["sum", "mean", "max", "min"], +) +@pytest.mark.parametrize("constructor", [Series, DataFrame]) +def test_expanding_func(func, static_comp, constructor): + data = constructor(np.array(list(range(10)) + [np.nan] * 10)) + result = getattr(data.expanding(min_periods=1, axis=0), func)() + assert isinstance(result, constructor) + + if constructor is Series: + tm.assert_almost_equal(result[10], static_comp(data[:11])) + else: + tm.assert_series_equal( + result.iloc[10], static_comp(data[:11]), check_names=False + ) + + @pytest.mark.parametrize("has_min_periods", [True, False]) @pytest.mark.parametrize( "func,static_comp", @@ -122,14 +141,6 @@ def expanding_func(x, min_periods=1, axis=0): exp = x.expanding(min_periods=min_periods, axis=axis) return getattr(exp, func)() - _check_expanding( - expanding_func, - static_comp, - preserve_nan=False, - series=series, - frame=frame, - nan_locs=nan_locs, - ) _check_expanding_has_min_periods(expanding_func, static_comp, has_min_periods) From 7c7fe515ba22a9bdd0da9ec87c21379ff06c8165 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Tue, 17 Nov 2020 17:23:19 -0800 Subject: [PATCH 3/5] Use more pytest idioms in moments expanding --- .../test_moments_consistency_expanding.py | 138 +++++++++--------- 1 file changed, 71 insertions(+), 67 deletions(-) diff --git a/pandas/tests/window/moments/test_moments_consistency_expanding.py b/pandas/tests/window/moments/test_moments_consistency_expanding.py index 8c1fa968c1133..2bfee420af647 100644 --- a/pandas/tests/window/moments/test_moments_consistency_expanding.py +++ b/pandas/tests/window/moments/test_moments_consistency_expanding.py @@ -16,49 +16,6 @@ ) -def _check_expanding( - func, static_comp, preserve_nan=True, series=None, frame=None, nan_locs=None -): - - series_result = func(series) - assert isinstance(series_result, Series) - frame_result = func(frame) - assert isinstance(frame_result, DataFrame) - - result = func(series) - tm.assert_almost_equal(result[10], static_comp(series[:11])) - - if preserve_nan: - assert result.iloc[nan_locs].isna().all() - - -def _check_expanding_has_min_periods(func, static_comp, has_min_periods): - ser = Series(np.random.randn(50)) - - if has_min_periods: - result = func(ser, min_periods=30) - assert result[:29].isna().all() - tm.assert_almost_equal(result.iloc[-1], static_comp(ser[:50])) - - # min_periods is working correctly - result = func(ser, min_periods=15) - assert isna(result.iloc[13]) - assert notna(result.iloc[14]) - - ser2 = Series(np.random.randn(20)) - result = func(ser2, min_periods=5) - assert isna(result[3]) - assert notna(result[4]) - - # min_periods=0 - result0 = func(ser, min_periods=0) - result1 = func(ser, min_periods=1) - tm.assert_almost_equal(result0, result1) - else: - result = func(ser) - tm.assert_almost_equal(result.iloc[-1], static_comp(ser[:50])) - - def test_expanding_corr(series): A = series.dropna() B = (A + np.random.randn(len(A)))[:-5] @@ -130,42 +87,89 @@ def test_expanding_func(func, static_comp, constructor): ) -@pytest.mark.parametrize("has_min_periods", [True, False]) @pytest.mark.parametrize( "func,static_comp", [("sum", np.sum), ("mean", np.mean), ("max", np.max), ("min", np.min)], ids=["sum", "mean", "max", "min"], ) -def test_expanding_func(func, static_comp, has_min_periods, series, frame, nan_locs): - def expanding_func(x, min_periods=1, axis=0): - exp = x.expanding(min_periods=min_periods, axis=axis) - return getattr(exp, func)() +def test_expanding_min_periods(func, static_comp): + ser = Series(np.random.randn(50)) + + result = getattr(ser.expanding(min_periods=30, axis=0), func)() + assert result[:29].isna().all() + tm.assert_almost_equal(result.iloc[-1], static_comp(ser[:50])) + + # min_periods is working correctly + result = getattr(ser.expanding(min_periods=15, axis=0), func)() + assert isna(result.iloc[13]) + assert notna(result.iloc[14]) - _check_expanding_has_min_periods(expanding_func, static_comp, has_min_periods) + ser2 = Series(np.random.randn(20)) + result = getattr(ser2.expanding(min_periods=5, axis=0), func)() + assert isna(result[3]) + assert notna(result[4]) + # min_periods=0 + result0 = getattr(ser.expanding(min_periods=0, axis=0), func)() + result1 = getattr(ser.expanding(min_periods=1, axis=0), func)() + tm.assert_almost_equal(result0, result1) -@pytest.mark.parametrize("has_min_periods", [True, False]) -def test_expanding_apply(engine_and_raw, has_min_periods, series, frame, nan_locs): + result = getattr(ser.expanding(min_periods=1, axis=0), func)() + tm.assert_almost_equal(result.iloc[-1], static_comp(ser[:50])) + +@pytest.mark.parametrize("constructor", [Series, DataFrame]) +def test_expanding_apply(engine_and_raw, constructor): engine, raw = engine_and_raw + data = constructor(np.array(list(range(10)) + [np.nan] * 10)) + result = data.expanding(min_periods=1).apply( + lambda x: x.mean(), raw=raw, engine=engine + ) + assert isinstance(result, constructor) + + if constructor is Series: + tm.assert_almost_equal(result[9], np.mean(data[:11])) + else: + tm.assert_series_equal(result.iloc[9], np.mean(data[:11]), check_names=False) + + +def test_expanding_min_periods_apply(engine_and_raw): + engine, raw = engine_and_raw + ser = Series(np.random.randn(50)) + + result = ser.expanding(min_periods=30).apply( + lambda x: x.mean(), raw=raw, engine=engine + ) + assert result[:29].isna().all() + tm.assert_almost_equal(result.iloc[-1], np.mean(ser[:50])) + + # min_periods is working correctly + result = ser.expanding(min_periods=15).apply( + lambda x: x.mean(), raw=raw, engine=engine + ) + assert isna(result.iloc[13]) + assert notna(result.iloc[14]) + + ser2 = Series(np.random.randn(20)) + result = ser2.expanding(min_periods=5).apply( + lambda x: x.mean(), raw=raw, engine=engine + ) + assert isna(result[3]) + assert notna(result[4]) + + # min_periods=0 + result0 = ser.expanding(min_periods=0).apply( + lambda x: x.mean(), raw=raw, engine=engine + ) + result1 = ser.expanding(min_periods=1).apply( + lambda x: x.mean(), raw=raw, engine=engine + ) + tm.assert_almost_equal(result0, result1) - def expanding_mean(x, min_periods=1): - - exp = x.expanding(min_periods=min_periods) - result = exp.apply(lambda x: x.mean(), raw=raw, engine=engine) - return result - - # TODO(jreback), needed to add preserve_nan=False - # here to make this pass - _check_expanding( - expanding_mean, - np.mean, - preserve_nan=False, - series=series, - frame=frame, - nan_locs=nan_locs, + result = ser.expanding(min_periods=1).apply( + lambda x: x.mean(), raw=raw, engine=engine ) - _check_expanding_has_min_periods(expanding_mean, np.mean, has_min_periods) + tm.assert_almost_equal(result.iloc[-1], np.mean(ser[:50])) @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) From a877fe3fc0abd4065f1b759bfe48f32f85ba9bf3 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Tue, 17 Nov 2020 17:33:31 -0800 Subject: [PATCH 4/5] black --- pandas/tests/window/conftest.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/pandas/tests/window/conftest.py b/pandas/tests/window/conftest.py index 36bf9d8764bca..e1d7635b0a686 100644 --- a/pandas/tests/window/conftest.py +++ b/pandas/tests/window/conftest.py @@ -35,7 +35,18 @@ def win_types_special(request): @pytest.fixture( - params=["sum", "mean", "median", "max", "min", "var", "std", "kurt", "skew", "count"] + params=[ + "sum", + "mean", + "median", + "max", + "min", + "var", + "std", + "kurt", + "skew", + "count", + ] ) def arithmetic_win_operators(request): return request.param From 486e52f7e7141f2e874b3085fd186aaa7d41e9cc Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Wed, 18 Nov 2020 09:40:46 -0800 Subject: [PATCH 5/5] Use frame_or_series fixture --- .../test_moments_consistency_expanding.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/pandas/tests/window/moments/test_moments_consistency_expanding.py b/pandas/tests/window/moments/test_moments_consistency_expanding.py index 2bfee420af647..25a897545ce58 100644 --- a/pandas/tests/window/moments/test_moments_consistency_expanding.py +++ b/pandas/tests/window/moments/test_moments_consistency_expanding.py @@ -73,13 +73,12 @@ def test_expanding_corr_pairwise(frame): [("sum", np.sum), ("mean", np.mean), ("max", np.max), ("min", np.min)], ids=["sum", "mean", "max", "min"], ) -@pytest.mark.parametrize("constructor", [Series, DataFrame]) -def test_expanding_func(func, static_comp, constructor): - data = constructor(np.array(list(range(10)) + [np.nan] * 10)) +def test_expanding_func(func, static_comp, frame_or_series): + data = frame_or_series(np.array(list(range(10)) + [np.nan] * 10)) result = getattr(data.expanding(min_periods=1, axis=0), func)() - assert isinstance(result, constructor) + assert isinstance(result, frame_or_series) - if constructor is Series: + if frame_or_series is Series: tm.assert_almost_equal(result[10], static_comp(data[:11])) else: tm.assert_series_equal( @@ -118,16 +117,15 @@ def test_expanding_min_periods(func, static_comp): tm.assert_almost_equal(result.iloc[-1], static_comp(ser[:50])) -@pytest.mark.parametrize("constructor", [Series, DataFrame]) -def test_expanding_apply(engine_and_raw, constructor): +def test_expanding_apply(engine_and_raw, frame_or_series): engine, raw = engine_and_raw - data = constructor(np.array(list(range(10)) + [np.nan] * 10)) + data = frame_or_series(np.array(list(range(10)) + [np.nan] * 10)) result = data.expanding(min_periods=1).apply( lambda x: x.mean(), raw=raw, engine=engine ) - assert isinstance(result, constructor) + assert isinstance(result, frame_or_series) - if constructor is Series: + if frame_or_series is Series: tm.assert_almost_equal(result[9], np.mean(data[:11])) else: tm.assert_series_equal(result.iloc[9], np.mean(data[:11]), check_names=False)