From 7e461a18d9f6928132afec6f48ce968b3e989ba6 Mon Sep 17 00:00:00 2001 From: Kaiqi Dong Date: Mon, 3 Dec 2018 17:43:52 +0100 Subject: [PATCH 1/9] remove \n from docstring --- pandas/core/arrays/datetimes.py | 26 +++++++++++++------------- pandas/core/arrays/timedeltas.py | 16 ++++++++-------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index cfe3afcf3730a..b3df505d56d78 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -82,7 +82,7 @@ def f(self): return result f.__name__ = name - f.__doc__ = docstring + f.__doc__ = "\n{}\n".format(docstring) return property(f) @@ -1072,19 +1072,19 @@ def date(self): return tslib.ints_to_pydatetime(timestamps, box="date") - year = _field_accessor('year', 'Y', "\n The year of the datetime\n") + year = _field_accessor('year', 'Y', "The year of the datetime") month = _field_accessor('month', 'M', - "\n The month as January=1, December=12 \n") - day = _field_accessor('day', 'D', "\nThe days of the datetime\n") - hour = _field_accessor('hour', 'h', "\nThe hours of the datetime\n") - minute = _field_accessor('minute', 'm', "\nThe minutes of the datetime\n") - second = _field_accessor('second', 's', "\nThe seconds of the datetime\n") + "The month as January=1, December=12") + day = _field_accessor('day', 'D', "The days of the datetime") + hour = _field_accessor('hour', 'h', "The hours of the datetime") + minute = _field_accessor('minute', 'm', "The minutes of the datetime") + second = _field_accessor('second', 's', "The seconds of the datetime") microsecond = _field_accessor('microsecond', 'us', - "\nThe microseconds of the datetime\n") + "The microseconds of the datetime") nanosecond = _field_accessor('nanosecond', 'ns', - "\nThe nanoseconds of the datetime\n") + "The nanoseconds of the datetime") weekofyear = _field_accessor('weekofyear', 'woy', - "\nThe week ordinal of the year\n") + "The week ordinal of the year") week = weekofyear _dayofweek_doc = """ The day of the week with Monday=0, Sunday=6. @@ -1129,12 +1129,12 @@ def date(self): "The name of day in a week (ex: Friday)\n\n.. deprecated:: 0.23.0") dayofyear = _field_accessor('dayofyear', 'doy', - "\nThe ordinal day of the year\n") - quarter = _field_accessor('quarter', 'q', "\nThe quarter of the date\n") + "The ordinal day of the year") + quarter = _field_accessor('quarter', 'q', "The quarter of the date") days_in_month = _field_accessor( 'days_in_month', 'dim', - "\nThe number of days in the month\n") + "The number of days in the month") daysinmonth = days_in_month _is_month_doc = """ Indicates whether the date is the {first_or_last} day of the month. diff --git a/pandas/core/arrays/timedeltas.py b/pandas/core/arrays/timedeltas.py index 830283d31a929..4afc9f5483c2a 100644 --- a/pandas/core/arrays/timedeltas.py +++ b/pandas/core/arrays/timedeltas.py @@ -59,7 +59,7 @@ def f(self): return result f.__name__ = name - f.__doc__ = docstring + f.__doc__ = "\n{}\n".format(docstring) return property(f) @@ -684,16 +684,16 @@ def to_pytimedelta(self): return tslibs.ints_to_pytimedelta(self.asi8) days = _field_accessor("days", "days", - "\nNumber of days for each element.\n") + "Number of days for each element.") seconds = _field_accessor("seconds", "seconds", - "\nNumber of seconds (>= 0 and less than 1 day) " - "for each element.\n") + "Number of seconds (>= 0 and less than 1 day) " + "for each element.") microseconds = _field_accessor("microseconds", "microseconds", - "\nNumber of microseconds (>= 0 and less " - "than 1 second) for each element.\n") + "Number of microseconds (>= 0 and less " + "than 1 second) for each element.") nanoseconds = _field_accessor("nanoseconds", "nanoseconds", - "\nNumber of nanoseconds (>= 0 and less " - "than 1 microsecond) for each element.\n") + "Number of nanoseconds (>= 0 and less " + "than 1 microsecond) for each element.") @property def components(self): From dea38f24c0067ae3fe9484b837c9649714213bba Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Tue, 14 Jan 2020 21:26:31 +0100 Subject: [PATCH 2/9] fix issue 17038 --- pandas/core/reshape/pivot.py | 4 +++- pandas/tests/reshape/test_pivot.py | 20 ++++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/pandas/core/reshape/pivot.py b/pandas/core/reshape/pivot.py index b443ba142369c..9743d90f4dd04 100644 --- a/pandas/core/reshape/pivot.py +++ b/pandas/core/reshape/pivot.py @@ -117,7 +117,9 @@ def pivot_table( agged[v] = maybe_downcast_to_dtype(agged[v], data[v].dtype) table = agged - if table.index.nlevels > 1: + + # GH 17038, this check should only happen if index is specified + if table.index.nlevels > 1 and index: # Related GH #17123 # If index_names are integers, determine whether the integers refer # to the level position or name. diff --git a/pandas/tests/reshape/test_pivot.py b/pandas/tests/reshape/test_pivot.py index 743fc50c87e96..46a05123c9fdd 100644 --- a/pandas/tests/reshape/test_pivot.py +++ b/pandas/tests/reshape/test_pivot.py @@ -896,12 +896,6 @@ def _check_output( totals = table.loc[("All", ""), value_col] assert totals == self.data[value_col].mean() - # no rows - rtable = self.data.pivot_table( - columns=["AA", "BB"], margins=True, aggfunc=np.mean - ) - assert isinstance(rtable, Series) - table = self.data.pivot_table(index=["AA", "BB"], margins=True, aggfunc="mean") for item in ["DD", "EE", "FF"]: totals = table.loc[("All", ""), item] @@ -972,6 +966,20 @@ def test_pivot_integer_columns(self): tm.assert_frame_equal(table, table2, check_names=False) + @pytest.mark.parametrize("cols", [(1, 2), ("a", "b"), (1, "b"), ("a", 1)]) + def test_pivot_table_multiindex_only(self, cols): + # GH 17038 + df2 = DataFrame({cols[0]: [1, 2, 3], cols[1]: [1, 2, 3], "v": [4, 5, 6]}) + + result = df2.pivot_table(values="v", columns=cols) + expected = DataFrame( + [[4, 5, 6]], + columns=MultiIndex.from_tuples([(1, 1), (2, 2), (3, 3)], names=cols), + index=Index(["v"]), + ) + + tm.assert_frame_equal(result, expected) + def test_pivot_no_level_overlap(self): # GH #1181 From cd9e7ac3f31ffaf95cd628863df911dea9fa1248 Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Tue, 14 Jan 2020 21:29:43 +0100 Subject: [PATCH 3/9] revert change --- pandas/core/reshape/pivot.py | 3 +-- pandas/tests/reshape/test_pivot.py | 20 ++++++-------------- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/pandas/core/reshape/pivot.py b/pandas/core/reshape/pivot.py index 9743d90f4dd04..a7cdbb0da7a4e 100644 --- a/pandas/core/reshape/pivot.py +++ b/pandas/core/reshape/pivot.py @@ -118,8 +118,7 @@ def pivot_table( table = agged - # GH 17038, this check should only happen if index is specified - if table.index.nlevels > 1 and index: + if table.index.nlevels > 1: # Related GH #17123 # If index_names are integers, determine whether the integers refer # to the level position or name. diff --git a/pandas/tests/reshape/test_pivot.py b/pandas/tests/reshape/test_pivot.py index 46a05123c9fdd..743fc50c87e96 100644 --- a/pandas/tests/reshape/test_pivot.py +++ b/pandas/tests/reshape/test_pivot.py @@ -896,6 +896,12 @@ def _check_output( totals = table.loc[("All", ""), value_col] assert totals == self.data[value_col].mean() + # no rows + rtable = self.data.pivot_table( + columns=["AA", "BB"], margins=True, aggfunc=np.mean + ) + assert isinstance(rtable, Series) + table = self.data.pivot_table(index=["AA", "BB"], margins=True, aggfunc="mean") for item in ["DD", "EE", "FF"]: totals = table.loc[("All", ""), item] @@ -966,20 +972,6 @@ def test_pivot_integer_columns(self): tm.assert_frame_equal(table, table2, check_names=False) - @pytest.mark.parametrize("cols", [(1, 2), ("a", "b"), (1, "b"), ("a", 1)]) - def test_pivot_table_multiindex_only(self, cols): - # GH 17038 - df2 = DataFrame({cols[0]: [1, 2, 3], cols[1]: [1, 2, 3], "v": [4, 5, 6]}) - - result = df2.pivot_table(values="v", columns=cols) - expected = DataFrame( - [[4, 5, 6]], - columns=MultiIndex.from_tuples([(1, 1), (2, 2), (3, 3)], names=cols), - index=Index(["v"]), - ) - - tm.assert_frame_equal(result, expected) - def test_pivot_no_level_overlap(self): # GH #1181 From e5e912be0f596943067a7df812442764d311a086 Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Tue, 14 Jan 2020 21:30:16 +0100 Subject: [PATCH 4/9] revert change --- pandas/core/reshape/pivot.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/core/reshape/pivot.py b/pandas/core/reshape/pivot.py index a7cdbb0da7a4e..b443ba142369c 100644 --- a/pandas/core/reshape/pivot.py +++ b/pandas/core/reshape/pivot.py @@ -117,7 +117,6 @@ def pivot_table( agged[v] = maybe_downcast_to_dtype(agged[v], data[v].dtype) table = agged - if table.index.nlevels > 1: # Related GH #17123 # If index_names are integers, determine whether the integers refer From 883e9b8f2a4abd8280b9a7ad26d2a0f58f45340e Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Wed, 29 Apr 2020 20:03:10 +0200 Subject: [PATCH 5/9] parametrize data and deprivate methods --- pandas/tests/window/common.py | 305 ++++++------ .../tests/window/moments/test_moments_ewm.py | 448 +++++++++--------- .../window/moments/test_moments_expanding.py | 178 ++++--- .../window/moments/test_moments_rolling.py | 133 ++++-- 4 files changed, 574 insertions(+), 490 deletions(-) diff --git a/pandas/tests/window/common.py b/pandas/tests/window/common.py index 72a0d12edffaf..a8bb9512b5dd4 100644 --- a/pandas/tests/window/common.py +++ b/pandas/tests/window/common.py @@ -25,7 +25,7 @@ def _create_data(self): # create the data only once as we are not setting it -def _create_consistency_data(): +def create_consistency_data(): def create_series(): return [ Series(dtype=object), @@ -167,10 +167,8 @@ def no_nans(x): # data is a tuple(object, is_constant, no_nans) data = create_series() + create_dataframes() - return [(x, is_constant(x), no_nans(x)) for x in data] - - -_consistency_data = _create_consistency_data() + for x in data: + yield x, is_constant(x), no_nans(x) class ConsistencyBase(Base): @@ -210,154 +208,6 @@ class ConsistencyBase(Base): def _create_data(self): super()._create_data() - self.data = _consistency_data - - def _test_moments_consistency_mock_mean(self, mean, mock_mean): - for (x, is_constant, no_nans) in self.data: - mean_x = mean(x) - # check that correlation of a series with itself is either 1 or NaN - - if mock_mean: - # check that mean equals mock_mean - expected = mock_mean(x) - tm.assert_equal(mean_x, expected.astype("float64")) - - def _test_moments_consistency_is_constant(self, min_periods, count, mean, corr): - for (x, is_constant, no_nans) in self.data: - count_x = count(x) - mean_x = mean(x) - # check that correlation of a series with itself is either 1 or NaN - corr_x_x = corr(x, x) - - if is_constant: - exp = x.max() if isinstance(x, Series) else x.max().max() - - # check mean of constant series - expected = x * np.nan - expected[count_x >= max(min_periods, 1)] = exp - tm.assert_equal(mean_x, expected) - - # check correlation of constant series with itself is NaN - expected[:] = np.nan - tm.assert_equal(corr_x_x, expected) - - def _test_moments_consistency_var_debiasing_factors( - self, var_biased=None, var_unbiased=None, var_debiasing_factors=None - ): - for (x, is_constant, no_nans) in self.data: - if var_unbiased and var_biased and var_debiasing_factors: - # check variance debiasing factors - var_unbiased_x = var_unbiased(x) - var_biased_x = var_biased(x) - var_debiasing_factors_x = var_debiasing_factors(x) - tm.assert_equal(var_unbiased_x, var_biased_x * var_debiasing_factors_x) - - def _test_moments_consistency_var_data( - self, min_periods, count, mean, var_unbiased, var_biased - ): - for (x, is_constant, no_nans) in self.data: - count_x = count(x) - mean_x = mean(x) - for var in [var_biased, var_unbiased]: - var_x = var(x) - assert not (var_x < 0).any().any() - - if var is var_biased: - # check that biased var(x) == mean(x^2) - mean(x)^2 - mean_x2 = mean(x * x) - tm.assert_equal(var_x, mean_x2 - (mean_x * mean_x)) - - if is_constant: - # check that variance of constant series is identically 0 - assert not (var_x > 0).any().any() - expected = x * np.nan - expected[count_x >= max(min_periods, 1)] = 0.0 - if var is var_unbiased: - expected[count_x < 2] = np.nan - tm.assert_equal(var_x, expected) - - def _test_moments_consistency_std_data( - self, std_unbiased, var_unbiased, std_biased, var_biased - ): - for (x, is_constant, no_nans) in self.data: - for (std, var) in [(std_biased, var_biased), (std_unbiased, var_unbiased)]: - var_x = var(x) - std_x = std(x) - assert not (var_x < 0).any().any() - assert not (std_x < 0).any().any() - - # check that var(x) == std(x)^2 - tm.assert_equal(var_x, std_x * std_x) - - def _test_moments_consistency_cov_data( - self, cov_unbiased, var_unbiased, cov_biased, var_biased - ): - for (x, is_constant, no_nans) in self.data: - for (cov, var) in [(cov_biased, var_biased), (cov_unbiased, var_unbiased)]: - var_x = var(x) - assert not (var_x < 0).any().any() - if cov: - cov_x_x = cov(x, x) - assert not (cov_x_x < 0).any().any() - - # check that var(x) == cov(x, x) - tm.assert_equal(var_x, cov_x_x) - - def _test_moments_consistency_series_data( - self, - corr, - mean, - std_biased, - std_unbiased, - cov_unbiased, - var_unbiased, - var_biased, - cov_biased, - ): - for (x, is_constant, no_nans) in self.data: - if isinstance(x, Series): - y = x - mean_x = mean(x) - if not x.isna().equals(y.isna()): - # can only easily test two Series with similar - # structure - pass - - # check that cor(x, y) is symmetric - corr_x_y = corr(x, y) - corr_y_x = corr(y, x) - tm.assert_equal(corr_x_y, corr_y_x) - - for (std, var, cov) in [ - (std_biased, var_biased, cov_biased), - (std_unbiased, var_unbiased, cov_unbiased), - ]: - var_x = var(x) - std_x = std(x) - - if cov: - # check that cov(x, y) is symmetric - cov_x_y = cov(x, y) - cov_y_x = cov(y, x) - tm.assert_equal(cov_x_y, cov_y_x) - - # check that cov(x, y) == (var(x+y) - var(x) - - # var(y)) / 2 - var_x_plus_y = var(x + y) - var_y = var(y) - tm.assert_equal(cov_x_y, 0.5 * (var_x_plus_y - var_x - var_y)) - - # check that corr(x, y) == cov(x, y) / (std(x) * - # std(y)) - std_y = std(y) - tm.assert_equal(corr_x_y, cov_x_y / (std_x * std_y)) - - if cov is cov_biased: - # check that biased cov(x, y) == mean(x*y) - - # mean(x)*mean(y) - mean_y = mean(y) - mean_x_times_y = mean(x * y) - tm.assert_equal(cov_x_y, mean_x_times_y - (mean_x * mean_y)) def _check_pairwise_moment(self, dispatch, name, **kwargs): def get_result(obj, obj2=None): @@ -400,3 +250,152 @@ def check_binary_ew_min_periods(name, min_periods, A, B): Series([1.0]), Series([1.0]), 50, name=name, min_periods=min_periods ) tm.assert_series_equal(result, Series([np.NaN])) + + +def test_moments_consistency_mock_mean(x, mean, mock_mean): + mean_x = mean(x) + # check that correlation of a series with itself is either 1 or NaN + + if mock_mean: + # check that mean equals mock_mean + expected = mock_mean(x) + tm.assert_equal(mean_x, expected.astype("float64")) + + +def test_moments_consistency_is_constant( + x, is_constant, min_periods, count, mean, corr +): + count_x = count(x) + mean_x = mean(x) + # check that correlation of a series with itself is either 1 or NaN + corr_x_x = corr(x, x) + + if is_constant: + exp = x.max() if isinstance(x, Series) else x.max().max() + + # check mean of constant series + expected = x * np.nan + expected[count_x >= max(min_periods, 1)] = exp + tm.assert_equal(mean_x, expected) + + # check correlation of constant series with itself is NaN + expected[:] = np.nan + tm.assert_equal(corr_x_x, expected) + + +def test_moments_consistency_var_debiasing_factors( + x, var_biased=None, var_unbiased=None, var_debiasing_factors=None +): + if var_unbiased and var_biased and var_debiasing_factors: + # check variance debiasing factors + var_unbiased_x = var_unbiased(x) + var_biased_x = var_biased(x) + var_debiasing_factors_x = var_debiasing_factors(x) + tm.assert_equal(var_unbiased_x, var_biased_x * var_debiasing_factors_x) + + +def test_moments_consistency_var_data( + x, is_constant, min_periods, count, mean, var_unbiased, var_biased +): + count_x = count(x) + mean_x = mean(x) + for var in [var_biased, var_unbiased]: + var_x = var(x) + assert not (var_x < 0).any().any() + + if var is var_biased: + # check that biased var(x) == mean(x^2) - mean(x)^2 + mean_x2 = mean(x * x) + tm.assert_equal(var_x, mean_x2 - (mean_x * mean_x)) + + if is_constant: + # check that variance of constant series is identically 0 + assert not (var_x > 0).any().any() + expected = x * np.nan + expected[count_x >= max(min_periods, 1)] = 0.0 + if var is var_unbiased: + expected[count_x < 2] = np.nan + tm.assert_equal(var_x, expected) + + +def test_moments_consistency_std_data( + x, std_unbiased, var_unbiased, std_biased, var_biased +): + for (std, var) in [(std_biased, var_biased), (std_unbiased, var_unbiased)]: + var_x = var(x) + std_x = std(x) + assert not (var_x < 0).any().any() + assert not (std_x < 0).any().any() + + # check that var(x) == std(x)^2 + tm.assert_equal(var_x, std_x * std_x) + + +def test_moments_consistency_cov_data( + x, cov_unbiased, var_unbiased, cov_biased, var_biased +): + for (cov, var) in [(cov_biased, var_biased), (cov_unbiased, var_unbiased)]: + var_x = var(x) + assert not (var_x < 0).any().any() + if cov: + cov_x_x = cov(x, x) + assert not (cov_x_x < 0).any().any() + + # check that var(x) == cov(x, x) + tm.assert_equal(var_x, cov_x_x) + + +def test_moments_consistency_series_data( + x, + corr, + mean, + std_biased, + std_unbiased, + cov_unbiased, + var_unbiased, + var_biased, + cov_biased, +): + if isinstance(x, Series): + y = x + mean_x = mean(x) + if not x.isna().equals(y.isna()): + # can only easily test two Series with similar + # structure + pass + + # check that cor(x, y) is symmetric + corr_x_y = corr(x, y) + corr_y_x = corr(y, x) + tm.assert_equal(corr_x_y, corr_y_x) + + for (std, var, cov) in [ + (std_biased, var_biased, cov_biased), + (std_unbiased, var_unbiased, cov_unbiased), + ]: + var_x = var(x) + std_x = std(x) + + if cov: + # check that cov(x, y) is symmetric + cov_x_y = cov(x, y) + cov_y_x = cov(y, x) + tm.assert_equal(cov_x_y, cov_y_x) + + # check that cov(x, y) == (var(x+y) - var(x) - + # var(y)) / 2 + var_x_plus_y = var(x + y) + var_y = var(y) + tm.assert_equal(cov_x_y, 0.5 * (var_x_plus_y - var_x - var_y)) + + # check that corr(x, y) == cov(x, y) / (std(x) * + # std(y)) + std_y = std(y) + tm.assert_equal(corr_x_y, cov_x_y / (std_x * std_y)) + + if cov is cov_biased: + # check that biased cov(x, y) == mean(x*y) - + # mean(x)*mean(y) + mean_y = mean(y) + mean_x_times_y = mean(x * y) + tm.assert_equal(cov_x_y, mean_x_times_y - (mean_x * mean_y)) diff --git a/pandas/tests/window/moments/test_moments_ewm.py b/pandas/tests/window/moments/test_moments_ewm.py index 78b086927adfb..cd4eea884ede7 100644 --- a/pandas/tests/window/moments/test_moments_ewm.py +++ b/pandas/tests/window/moments/test_moments_ewm.py @@ -11,6 +11,14 @@ check_binary_ew, check_binary_ew_min_periods, ew_func, + create_consistency_data, + test_moments_consistency_var_data, + test_moments_consistency_var_debiasing_factors, + test_moments_consistency_is_constant, + test_moments_consistency_mock_mean, + test_moments_consistency_cov_data, + test_moments_consistency_series_data, + test_moments_consistency_std_data, ) @@ -293,227 +301,241 @@ def test_different_input_array_raise_exception(self, name, binary_ew_data): with pytest.raises(Exception, match=msg): ew_func(A, randn(50), 20, name=name, min_periods=5) - @pytest.mark.slow - @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) - @pytest.mark.parametrize("adjust", [True, False]) - @pytest.mark.parametrize("ignore_na", [True, False]) - def test_ewm_consistency(self, min_periods, adjust, ignore_na): - def _weights(s, com, adjust, ignore_na): - if isinstance(s, DataFrame): - if not len(s.columns): - return DataFrame(index=s.index, columns=s.columns) - w = concat( - [ - _weights( - s.iloc[:, i], com=com, adjust=adjust, ignore_na=ignore_na - ) - for i, _ in enumerate(s.columns) - ], - axis=1, - ) - w.index = s.index - w.columns = s.columns - return w - - w = Series(np.nan, index=s.index) - alpha = 1.0 / (1.0 + com) - if ignore_na: - w[s.notna()] = _weights( - s[s.notna()], com=com, adjust=adjust, ignore_na=False - ) - elif adjust: - for i in range(len(s)): - if s.iat[i] == s.iat[i]: - w.iat[i] = pow(1.0 / (1.0 - alpha), i) - else: - sum_wts = 0.0 - prev_i = -1 - for i in range(len(s)): - if s.iat[i] == s.iat[i]: - if prev_i == -1: - w.iat[i] = 1.0 - else: - w.iat[i] = alpha * sum_wts / pow(1.0 - alpha, i - prev_i) - sum_wts += w.iat[i] - prev_i = i + +@pytest.mark.slow +@pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) +@pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) +@pytest.mark.parametrize("adjust", [True, False]) +@pytest.mark.parametrize("ignore_na", [True, False]) +def test_ewm_consistency(x, is_constant, no_nans, min_periods, adjust, ignore_na): + def _weights(s, com, adjust, ignore_na): + if isinstance(s, DataFrame): + if not len(s.columns): + return DataFrame(index=s.index, columns=s.columns) + w = concat( + [ + _weights(s.iloc[:, i], com=com, adjust=adjust, ignore_na=ignore_na) + for i, _ in enumerate(s.columns) + ], + axis=1, + ) + w.index = s.index + w.columns = s.columns return w - def _variance_debiasing_factors(s, com, adjust, ignore_na): - weights = _weights(s, com=com, adjust=adjust, ignore_na=ignore_na) - cum_sum = weights.cumsum().fillna(method="ffill") - cum_sum_sq = (weights * weights).cumsum().fillna(method="ffill") - numerator = cum_sum * cum_sum - denominator = numerator - cum_sum_sq - denominator[denominator <= 0.0] = np.nan - return numerator / denominator - - def _ewma(s, com, min_periods, adjust, ignore_na): - weights = _weights(s, com=com, adjust=adjust, ignore_na=ignore_na) - result = ( - s.multiply(weights) - .cumsum() - .divide(weights.cumsum()) - .fillna(method="ffill") + w = Series(np.nan, index=s.index) + alpha = 1.0 / (1.0 + com) + if ignore_na: + w[s.notna()] = _weights( + s[s.notna()], com=com, adjust=adjust, ignore_na=False ) - result[ - s.expanding().count() < (max(min_periods, 1) if min_periods else 1) - ] = np.nan - return result - - com = 3.0 - self._test_moments_consistency_mock_mean( - mean=lambda x: x.ewm( - com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na - ).mean(), - mock_mean=lambda x: _ewma( - x, com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na - ), + elif adjust: + for i in range(len(s)): + if s.iat[i] == s.iat[i]: + w.iat[i] = pow(1.0 / (1.0 - alpha), i) + else: + sum_wts = 0.0 + prev_i = -1 + for i in range(len(s)): + if s.iat[i] == s.iat[i]: + if prev_i == -1: + w.iat[i] = 1.0 + else: + w.iat[i] = alpha * sum_wts / pow(1.0 - alpha, i - prev_i) + sum_wts += w.iat[i] + prev_i = i + return w + + def _variance_debiasing_factors(s, com, adjust, ignore_na): + weights = _weights(s, com=com, adjust=adjust, ignore_na=ignore_na) + cum_sum = weights.cumsum().fillna(method="ffill") + cum_sum_sq = (weights * weights).cumsum().fillna(method="ffill") + numerator = cum_sum * cum_sum + denominator = numerator - cum_sum_sq + denominator[denominator <= 0.0] = np.nan + return numerator / denominator + + def _ewma(s, com, min_periods, adjust, ignore_na): + weights = _weights(s, com=com, adjust=adjust, ignore_na=ignore_na) + result = ( + s.multiply(weights).cumsum().divide(weights.cumsum()).fillna(method="ffill") ) - - self._test_moments_consistency_is_constant( - min_periods=min_periods, - count=lambda x: x.expanding().count(), - mean=lambda x: x.ewm( + result[ + s.expanding().count() < (max(min_periods, 1) if min_periods else 1) + ] = np.nan + return result + + com = 3.0 + test_moments_consistency_mock_mean( + x=x, + mean=lambda x: x.ewm( + com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na + ).mean(), + mock_mean=lambda x: _ewma( + x, com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na + ), + ) + + test_moments_consistency_is_constant( + x=x, + is_constant=is_constant, + min_periods=min_periods, + count=lambda x: x.expanding().count(), + mean=lambda x: x.ewm( + com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na + ).mean(), + corr=lambda x, y: x.ewm( + com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na + ).corr(y), + ) + + test_moments_consistency_var_debiasing_factors( + x=x, + var_unbiased=lambda x: ( + x.ewm( com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na - ).mean(), - corr=lambda x, y: x.ewm( + ).var(bias=False) + ), + var_biased=lambda x: ( + x.ewm( com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na - ).corr(y), - ) - - self._test_moments_consistency_var_debiasing_factors( - var_unbiased=lambda x: ( - x.ewm( - com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na - ).var(bias=False) - ), - var_biased=lambda x: ( - x.ewm( - com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na - ).var(bias=True) - ), - var_debiasing_factors=lambda x: ( - _variance_debiasing_factors( - x, com=com, adjust=adjust, ignore_na=ignore_na - ) - ), - ) - - @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) - @pytest.mark.parametrize("adjust", [True, False]) - @pytest.mark.parametrize("ignore_na", [True, False]) - def test_ewm_consistency_var(self, min_periods, adjust, ignore_na): - com = 3.0 - self._test_moments_consistency_var_data( - min_periods, - count=lambda x: x.expanding().count(), - mean=lambda x: x.ewm( + ).var(bias=True) + ), + var_debiasing_factors=lambda x: ( + _variance_debiasing_factors(x, com=com, adjust=adjust, ignore_na=ignore_na) + ), + ) + + +@pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) +@pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) +@pytest.mark.parametrize("adjust", [True, False]) +@pytest.mark.parametrize("ignore_na", [True, False]) +def test_ewm_consistency_var(x, is_constant, no_nans, min_periods, adjust, ignore_na): + com = 3.0 + test_moments_consistency_var_data( + x=x, + is_constant=is_constant, + min_periods=min_periods, + count=lambda x: x.expanding().count(), + mean=lambda x: x.ewm( + com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na + ).mean(), + var_unbiased=lambda x: ( + x.ewm( com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na - ).mean(), - var_unbiased=lambda x: ( - x.ewm( - com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na - ).var(bias=False) - ), - var_biased=lambda x: ( - x.ewm( - com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na - ).var(bias=True) - ), - ) - - @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) - @pytest.mark.parametrize("adjust", [True, False]) - @pytest.mark.parametrize("ignore_na", [True, False]) - def test_ewm_consistency_std(self, min_periods, adjust, ignore_na): - com = 3.0 - self._test_moments_consistency_std_data( - var_unbiased=lambda x: ( - x.ewm( - com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na - ).var(bias=False) - ), - std_unbiased=lambda x: ( - x.ewm( - com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na - ).std(bias=False) - ), - var_biased=lambda x: ( - x.ewm( - com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na - ).var(bias=True) - ), - std_biased=lambda x: x.ewm( + ).var(bias=False) + ), + var_biased=lambda x: ( + x.ewm( com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na - ).std(bias=True), - ) - - @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) - @pytest.mark.parametrize("adjust", [True, False]) - @pytest.mark.parametrize("ignore_na", [True, False]) - def test_ewm_consistency_cov(self, min_periods, adjust, ignore_na): - com = 3.0 - self._test_moments_consistency_cov_data( - var_unbiased=lambda x: ( - x.ewm( - com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na - ).var(bias=False) - ), - cov_unbiased=lambda x, y: ( - x.ewm( - com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na - ).cov(y, bias=False) - ), - var_biased=lambda x: ( - x.ewm( - com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na - ).var(bias=True) - ), - cov_biased=lambda x, y: ( - x.ewm( - com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na - ).cov(y, bias=True) - ), - ) - - @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) - @pytest.mark.parametrize("adjust", [True, False]) - @pytest.mark.parametrize("ignore_na", [True, False]) - def test_ewm_consistency_series_data(self, min_periods, adjust, ignore_na): - com = 3.0 - self._test_moments_consistency_series_data( - mean=lambda x: x.ewm( + ).var(bias=True) + ), + ) + + +@pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) +@pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) +@pytest.mark.parametrize("adjust", [True, False]) +@pytest.mark.parametrize("ignore_na", [True, False]) +def test_ewm_consistency_std(x, is_constant, no_nans, min_periods, adjust, ignore_na): + com = 3.0 + test_moments_consistency_std_data( + x=x, + var_unbiased=lambda x: ( + x.ewm( com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na - ).mean(), - corr=lambda x, y: x.ewm( + ).var(bias=False) + ), + std_unbiased=lambda x: ( + x.ewm( com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na - ).corr(y), - var_unbiased=lambda x: ( - x.ewm( - com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na - ).var(bias=False) - ), - std_unbiased=lambda x: ( - x.ewm( - com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na - ).std(bias=False) - ), - cov_unbiased=lambda x, y: ( - x.ewm( - com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na - ).cov(y, bias=False) - ), - var_biased=lambda x: ( - x.ewm( - com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na - ).var(bias=True) - ), - std_biased=lambda x: x.ewm( + ).std(bias=False) + ), + var_biased=lambda x: ( + x.ewm( com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na - ).std(bias=True), - cov_biased=lambda x, y: ( - x.ewm( - com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na - ).cov(y, bias=True) - ), - ) + ).var(bias=True) + ), + std_biased=lambda x: x.ewm( + com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na + ).std(bias=True), + ) + + +@pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) +@pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) +@pytest.mark.parametrize("adjust", [True, False]) +@pytest.mark.parametrize("ignore_na", [True, False]) +def test_ewm_consistency_cov(x, is_constant, no_nans, min_periods, adjust, ignore_na): + com = 3.0 + test_moments_consistency_cov_data( + x=x, + var_unbiased=lambda x: ( + x.ewm( + com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na + ).var(bias=False) + ), + cov_unbiased=lambda x, y: ( + x.ewm( + com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na + ).cov(y, bias=False) + ), + var_biased=lambda x: ( + x.ewm( + com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na + ).var(bias=True) + ), + cov_biased=lambda x, y: ( + x.ewm( + com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na + ).cov(y, bias=True) + ), + ) + + +@pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) +@pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) +@pytest.mark.parametrize("adjust", [True, False]) +@pytest.mark.parametrize("ignore_na", [True, False]) +def test_ewm_consistency_series_data( + x, is_constant, no_nans, min_periods, adjust, ignore_na +): + com = 3.0 + test_moments_consistency_series_data( + x=x, + mean=lambda x: x.ewm( + com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na + ).mean(), + corr=lambda x, y: x.ewm( + com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na + ).corr(y), + var_unbiased=lambda x: ( + x.ewm( + com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na + ).var(bias=False) + ), + std_unbiased=lambda x: ( + x.ewm( + com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na + ).std(bias=False) + ), + cov_unbiased=lambda x, y: ( + x.ewm( + com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na + ).cov(y, bias=False) + ), + var_biased=lambda x: ( + x.ewm( + com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na + ).var(bias=True) + ), + std_biased=lambda x: x.ewm( + com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na + ).std(bias=True), + cov_biased=lambda x, y: ( + x.ewm( + com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na + ).cov(y, bias=True) + ), + ) diff --git a/pandas/tests/window/moments/test_moments_expanding.py b/pandas/tests/window/moments/test_moments_expanding.py index a358f0e7057f3..8d826ca809c48 100644 --- a/pandas/tests/window/moments/test_moments_expanding.py +++ b/pandas/tests/window/moments/test_moments_expanding.py @@ -6,7 +6,17 @@ from pandas import DataFrame, Index, MultiIndex, Series, isna, notna import pandas._testing as tm -from pandas.tests.window.common import ConsistencyBase +from pandas.tests.window.common import ( + ConsistencyBase, + create_consistency_data, + test_moments_consistency_var_data, + test_moments_consistency_var_debiasing_factors, + test_moments_consistency_is_constant, + test_moments_consistency_mock_mean, + test_moments_consistency_cov_data, + test_moments_consistency_series_data, + test_moments_consistency_std_data, +) class TestExpandingMomentsConsistency(ConsistencyBase): @@ -333,8 +343,9 @@ def test_moment_functions_zero_length_pairwise(self, f): tm.assert_frame_equal(df2_result, df2_expected) @pytest.mark.slow + @pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) - def test_expanding_consistency(self, min_periods): + def test_expanding_consistency(self, x, is_constant, no_nans, min_periods): # suppress warnings about empty slices, as we are deliberately testing # with empty/0-length Series/DataFrames @@ -346,20 +357,24 @@ def test_expanding_consistency(self, min_periods): ) # test consistency between different expanding_* moments - self._test_moments_consistency_mock_mean( + test_moments_consistency_mock_mean( + x=x, mean=lambda x: x.expanding(min_periods=min_periods).mean(), mock_mean=lambda x: x.expanding(min_periods=min_periods).sum() / x.expanding().count(), ) - self._test_moments_consistency_is_constant( + test_moments_consistency_is_constant( + x=x, + is_constant=is_constant, min_periods=min_periods, count=lambda x: x.expanding().count(), mean=lambda x: x.expanding(min_periods=min_periods).mean(), corr=lambda x, y: x.expanding(min_periods=min_periods).corr(y), ) - self._test_moments_consistency_var_debiasing_factors( + test_moments_consistency_var_debiasing_factors( + x=x, var_unbiased=lambda x: x.expanding(min_periods=min_periods).var(), var_biased=lambda x: x.expanding(min_periods=min_periods).var(ddof=0), var_debiasing_factors=lambda x: ( @@ -368,8 +383,9 @@ def test_expanding_consistency(self, min_periods): ), ) + @pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) - def test_expanding_apply_consistency(self, min_periods): + def test_expanding_apply_consistency(self, x, is_constant, no_nans, min_periods): with warnings.catch_warnings(): warnings.filterwarnings( "ignore", @@ -379,77 +395,89 @@ def test_expanding_apply_consistency(self, min_periods): # test consistency between expanding_xyz() and either (a) # expanding_apply of Series.xyz(), or (b) expanding_apply of # np.nanxyz() - for (x, is_constant, no_nans) in self.data: - functions = self.base_functions - - # GH 8269 - if no_nans: - functions = self.base_functions + self.no_nan_functions - for (f, require_min_periods, name) in functions: - expanding_f = getattr(x.expanding(min_periods=min_periods), name) - - if ( - require_min_periods - and (min_periods is not None) - and (min_periods < require_min_periods) - ): - continue - - if name == "count": - expanding_f_result = expanding_f() - expanding_apply_f_result = x.expanding(min_periods=0).apply( - func=f, raw=True - ) + functions = self.base_functions + + # GH 8269 + if no_nans: + functions = self.base_functions + self.no_nan_functions + for (f, require_min_periods, name) in functions: + expanding_f = getattr(x.expanding(min_periods=min_periods), name) + + if ( + require_min_periods + and (min_periods is not None) + and (min_periods < require_min_periods) + ): + continue + + if name == "count": + expanding_f_result = expanding_f() + expanding_apply_f_result = x.expanding(min_periods=0).apply( + func=f, raw=True + ) + else: + if name in ["cov", "corr"]: + expanding_f_result = expanding_f(pairwise=False) else: - if name in ["cov", "corr"]: - expanding_f_result = expanding_f(pairwise=False) - else: - expanding_f_result = expanding_f() - expanding_apply_f_result = x.expanding( - min_periods=min_periods - ).apply(func=f, raw=True) - - # GH 9422 - if name in ["sum", "prod"]: - tm.assert_equal(expanding_f_result, expanding_apply_f_result) + expanding_f_result = expanding_f() + expanding_apply_f_result = x.expanding( + min_periods=min_periods + ).apply(func=f, raw=True) + + # GH 9422 + if name in ["sum", "prod"]: + tm.assert_equal(expanding_f_result, expanding_apply_f_result) + + +@pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) +@pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) +def test_moments_consistency_var(x, is_constant, no_nans, min_periods): + test_moments_consistency_var_data( + x=x, + is_constant=is_constant, + min_periods=min_periods, + count=lambda x: x.expanding(min_periods=min_periods).count(), + mean=lambda x: x.expanding(min_periods=min_periods).mean(), + var_unbiased=lambda x: x.expanding(min_periods=min_periods).var(), + var_biased=lambda x: x.expanding(min_periods=min_periods).var(ddof=0), + ) - @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) - def test_moments_consistency_var(self, min_periods): - self._test_moments_consistency_var_data( - min_periods=min_periods, - count=lambda x: x.expanding(min_periods=min_periods).count(), - mean=lambda x: x.expanding(min_periods=min_periods).mean(), - var_unbiased=lambda x: x.expanding(min_periods=min_periods).var(), - var_biased=lambda x: x.expanding(min_periods=min_periods).var(ddof=0), - ) - @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) - def test_expanding_consistency_std(self, min_periods): - self._test_moments_consistency_std_data( - var_unbiased=lambda x: x.expanding(min_periods=min_periods).var(), - std_unbiased=lambda x: x.expanding(min_periods=min_periods).std(), - var_biased=lambda x: x.expanding(min_periods=min_periods).var(ddof=0), - std_biased=lambda x: x.expanding(min_periods=min_periods).std(ddof=0), - ) +@pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) +@pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) +def test_expanding_consistency_std(x, is_constant, no_nans, min_periods): + test_moments_consistency_std_data( + x=x, + var_unbiased=lambda x: x.expanding(min_periods=min_periods).var(), + std_unbiased=lambda x: x.expanding(min_periods=min_periods).std(), + var_biased=lambda x: x.expanding(min_periods=min_periods).var(ddof=0), + std_biased=lambda x: x.expanding(min_periods=min_periods).std(ddof=0), + ) - @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) - def test_expanding_consistency_cov(self, min_periods): - self._test_moments_consistency_cov_data( - var_unbiased=lambda x: x.expanding(min_periods=min_periods).var(), - cov_unbiased=lambda x, y: x.expanding(min_periods=min_periods).cov(y), - var_biased=lambda x: x.expanding(min_periods=min_periods).var(ddof=0), - cov_biased=lambda x, y: x.expanding(min_periods=min_periods).cov(y, ddof=0), - ) - @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) - def test_expanding_consistency_series(self, min_periods): - self._test_moments_consistency_series_data( - mean=lambda x: x.expanding(min_periods=min_periods).mean(), - corr=lambda x, y: x.expanding(min_periods=min_periods).corr(y), - var_unbiased=lambda x: x.expanding(min_periods=min_periods).var(), - std_unbiased=lambda x: x.expanding(min_periods=min_periods).std(), - cov_unbiased=lambda x, y: x.expanding(min_periods=min_periods).cov(y), - var_biased=lambda x: x.expanding(min_periods=min_periods).var(ddof=0), - std_biased=lambda x: x.expanding(min_periods=min_periods).std(ddof=0), - cov_biased=lambda x, y: x.expanding(min_periods=min_periods).cov(y, ddof=0), - ) +@pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) +@pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) +def test_expanding_consistency_cov(x, is_constant, no_nans, min_periods): + test_moments_consistency_cov_data( + x=x, + var_unbiased=lambda x: x.expanding(min_periods=min_periods).var(), + cov_unbiased=lambda x, y: x.expanding(min_periods=min_periods).cov(y), + var_biased=lambda x: x.expanding(min_periods=min_periods).var(ddof=0), + cov_biased=lambda x, y: x.expanding(min_periods=min_periods).cov(y, ddof=0), + ) + + +@pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) +@pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) +def test_expanding_consistency_series(x, is_constant, no_nans, min_periods): + test_moments_consistency_series_data( + x=x, + mean=lambda x: x.expanding(min_periods=min_periods).mean(), + corr=lambda x, y: x.expanding(min_periods=min_periods).corr(y), + var_unbiased=lambda x: x.expanding(min_periods=min_periods).var(), + std_unbiased=lambda x: x.expanding(min_periods=min_periods).std(), + cov_unbiased=lambda x, y: x.expanding(min_periods=min_periods).cov(y), + var_biased=lambda x: x.expanding(min_periods=min_periods).var(ddof=0), + std_biased=lambda x: x.expanding(min_periods=min_periods).std(ddof=0), + cov_biased=lambda x, y: x.expanding(min_periods=min_periods).cov(y, ddof=0), + ) diff --git a/pandas/tests/window/moments/test_moments_rolling.py b/pandas/tests/window/moments/test_moments_rolling.py index 503f2e12bea03..5ec5f469e4141 100644 --- a/pandas/tests/window/moments/test_moments_rolling.py +++ b/pandas/tests/window/moments/test_moments_rolling.py @@ -12,7 +12,18 @@ from pandas import DataFrame, DatetimeIndex, Index, Series, isna, notna import pandas._testing as tm from pandas.core.window.common import _flex_binary_moment -from pandas.tests.window.common import Base, ConsistencyBase +from pandas.tests.window.common import ( + Base, + ConsistencyBase, + create_consistency_data, + test_moments_consistency_var_data, + test_moments_consistency_var_debiasing_factors, + test_moments_consistency_is_constant, + test_moments_consistency_mock_mean, + test_moments_consistency_cov_data, + test_moments_consistency_series_data, + test_moments_consistency_std_data, +) import pandas.tseries.offsets as offsets @@ -937,10 +948,13 @@ def setup_method(self, method): self._create_data() @pytest.mark.slow + @pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) @pytest.mark.parametrize( "window,min_periods,center", list(_rolling_consistency_cases()) ) - def test_rolling_consistency(self, window, min_periods, center): + def test_rolling_consistency( + self, x, is_constant, no_nans, window, min_periods, center + ): # suppress warnings about empty slices, as we are deliberately testing # with empty/0-length Series/DataFrames @@ -952,7 +966,8 @@ def test_rolling_consistency(self, window, min_periods, center): ) # test consistency between different rolling_* moments - self._test_moments_consistency_mock_mean( + test_moments_consistency_mock_mean( + x=x, mean=lambda x: ( x.rolling( window=window, min_periods=min_periods, center=center @@ -969,7 +984,9 @@ def test_rolling_consistency(self, window, min_periods, center): ), ) - self._test_moments_consistency_is_constant( + test_moments_consistency_is_constant( + x=x, + is_constant=is_constant, min_periods=min_periods, count=lambda x: ( x.rolling( @@ -988,7 +1005,8 @@ def test_rolling_consistency(self, window, min_periods, center): ), ) - self._test_moments_consistency_var_debiasing_factors( + test_moments_consistency_var_debiasing_factors( + x=x, var_unbiased=lambda x: ( x.rolling( window=window, min_periods=min_periods, center=center @@ -1013,10 +1031,13 @@ def test_rolling_consistency(self, window, min_periods, center): ), ) + @pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) @pytest.mark.parametrize( "window,min_periods,center", list(_rolling_consistency_cases()) ) - def test_rolling_apply_consistency(self, window, min_periods, center): + def test_rolling_apply_consistency( + self, x, is_constant, no_nans, window, min_periods, center + ): with warnings.catch_warnings(): warnings.filterwarnings( @@ -1027,51 +1048,53 @@ def test_rolling_apply_consistency(self, window, min_periods, center): # test consistency between rolling_xyz() and either (a) # rolling_apply of Series.xyz(), or (b) rolling_apply of # np.nanxyz() - for (x, is_constant, no_nans) in self.data: - functions = self.base_functions - - # GH 8269 - if no_nans: - functions = self.base_functions + self.no_nan_functions - for (f, require_min_periods, name) in functions: - rolling_f = getattr( - x.rolling( - window=window, center=center, min_periods=min_periods - ), - name, - ) + functions = self.base_functions + + # GH 8269 + if no_nans: + functions = self.base_functions + self.no_nan_functions + for (f, require_min_periods, name) in functions: + rolling_f = getattr( + x.rolling(window=window, center=center, min_periods=min_periods), + name, + ) - if ( - require_min_periods - and (min_periods is not None) - and (min_periods < require_min_periods) - ): - continue + if ( + require_min_periods + and (min_periods is not None) + and (min_periods < require_min_periods) + ): + continue - if name == "count": - rolling_f_result = rolling_f() - rolling_apply_f_result = x.rolling( - window=window, min_periods=min_periods, center=center - ).apply(func=f, raw=True) + if name == "count": + rolling_f_result = rolling_f() + rolling_apply_f_result = x.rolling( + window=window, min_periods=min_periods, center=center + ).apply(func=f, raw=True) + else: + if name in ["cov", "corr"]: + rolling_f_result = rolling_f(pairwise=False) else: - if name in ["cov", "corr"]: - rolling_f_result = rolling_f(pairwise=False) - else: - rolling_f_result = rolling_f() - rolling_apply_f_result = x.rolling( - window=window, min_periods=min_periods, center=center - ).apply(func=f, raw=True) + rolling_f_result = rolling_f() + rolling_apply_f_result = x.rolling( + window=window, min_periods=min_periods, center=center + ).apply(func=f, raw=True) - # GH 9422 - if name in ["sum", "prod"]: - tm.assert_equal(rolling_f_result, rolling_apply_f_result) + # GH 9422 + if name in ["sum", "prod"]: + tm.assert_equal(rolling_f_result, rolling_apply_f_result) + @pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) @pytest.mark.parametrize( "window,min_periods,center", list(_rolling_consistency_cases()) ) - def test_rolling_consistency_var(self, window, min_periods, center): - self._test_moments_consistency_var_data( - min_periods, + def test_rolling_consistency_var( + self, x, is_constant, no_nans, window, min_periods, center + ): + test_moments_consistency_var_data( + x=x, + is_constant=is_constant, + min_periods=min_periods, count=lambda x: ( x.rolling(window=window, min_periods=min_periods, center=center).count() ), @@ -1088,11 +1111,15 @@ def test_rolling_consistency_var(self, window, min_periods, center): ), ) + @pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) @pytest.mark.parametrize( "window,min_periods,center", list(_rolling_consistency_cases()) ) - def test_rolling_consistency_std(self, window, min_periods, center): - self._test_moments_consistency_std_data( + def test_rolling_consistency_std( + self, x, is_constant, no_nans, window, min_periods, center + ): + test_moments_consistency_std_data( + x=x, var_unbiased=lambda x: ( x.rolling(window=window, min_periods=min_periods, center=center).var() ), @@ -1111,11 +1138,15 @@ def test_rolling_consistency_std(self, window, min_periods, center): ), ) + @pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) @pytest.mark.parametrize( "window,min_periods,center", list(_rolling_consistency_cases()) ) - def test_rolling_consistency_cov(self, window, min_periods, center): - self._test_moments_consistency_cov_data( + def test_rolling_consistency_cov( + self, x, is_constant, no_nans, window, min_periods, center + ): + test_moments_consistency_cov_data( + x=x, var_unbiased=lambda x: ( x.rolling(window=window, min_periods=min_periods, center=center).var() ), @@ -1134,11 +1165,15 @@ def test_rolling_consistency_cov(self, window, min_periods, center): ), ) + @pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) @pytest.mark.parametrize( "window,min_periods,center", list(_rolling_consistency_cases()) ) - def test_rolling_consistency_series(self, window, min_periods, center): - self._test_moments_consistency_series_data( + def test_rolling_consistency_series( + self, x, is_constant, no_nans, window, min_periods, center + ): + test_moments_consistency_series_data( + x=x, mean=lambda x: ( x.rolling(window=window, min_periods=min_periods, center=center).mean() ), From 893fddf6dc19ee31d7e2af35afcc04970a40e982 Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Wed, 29 Apr 2020 20:26:09 +0200 Subject: [PATCH 6/9] fix isort --- pandas/tests/window/moments/test_moments_ewm.py | 8 ++++---- pandas/tests/window/moments/test_moments_expanding.py | 6 +++--- pandas/tests/window/moments/test_moments_rolling.py | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/pandas/tests/window/moments/test_moments_ewm.py b/pandas/tests/window/moments/test_moments_ewm.py index cd4eea884ede7..853120b96231b 100644 --- a/pandas/tests/window/moments/test_moments_ewm.py +++ b/pandas/tests/window/moments/test_moments_ewm.py @@ -10,15 +10,15 @@ ConsistencyBase, check_binary_ew, check_binary_ew_min_periods, - ew_func, create_consistency_data, - test_moments_consistency_var_data, - test_moments_consistency_var_debiasing_factors, + ew_func, + test_moments_consistency_cov_data, test_moments_consistency_is_constant, test_moments_consistency_mock_mean, - test_moments_consistency_cov_data, test_moments_consistency_series_data, test_moments_consistency_std_data, + test_moments_consistency_var_data, + test_moments_consistency_var_debiasing_factors, ) diff --git a/pandas/tests/window/moments/test_moments_expanding.py b/pandas/tests/window/moments/test_moments_expanding.py index 8d826ca809c48..d8e4e3b98733a 100644 --- a/pandas/tests/window/moments/test_moments_expanding.py +++ b/pandas/tests/window/moments/test_moments_expanding.py @@ -9,13 +9,13 @@ from pandas.tests.window.common import ( ConsistencyBase, create_consistency_data, - test_moments_consistency_var_data, - test_moments_consistency_var_debiasing_factors, + test_moments_consistency_cov_data, test_moments_consistency_is_constant, test_moments_consistency_mock_mean, - test_moments_consistency_cov_data, test_moments_consistency_series_data, test_moments_consistency_std_data, + test_moments_consistency_var_data, + test_moments_consistency_var_debiasing_factors, ) diff --git a/pandas/tests/window/moments/test_moments_rolling.py b/pandas/tests/window/moments/test_moments_rolling.py index 5ec5f469e4141..ced5f9f0d54f8 100644 --- a/pandas/tests/window/moments/test_moments_rolling.py +++ b/pandas/tests/window/moments/test_moments_rolling.py @@ -16,13 +16,13 @@ Base, ConsistencyBase, create_consistency_data, - test_moments_consistency_var_data, - test_moments_consistency_var_debiasing_factors, + test_moments_consistency_cov_data, test_moments_consistency_is_constant, test_moments_consistency_mock_mean, - test_moments_consistency_cov_data, test_moments_consistency_series_data, test_moments_consistency_std_data, + test_moments_consistency_var_data, + test_moments_consistency_var_debiasing_factors, ) import pandas.tseries.offsets as offsets From 52e8b6f42904bf34e90ea0fa4045b2bd4b7c2aac Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Wed, 29 Apr 2020 21:03:49 +0200 Subject: [PATCH 7/9] fixup --- pandas/tests/window/common.py | 30 +- .../tests/window/moments/test_moments_ewm.py | 40 +- .../window/moments/test_moments_expanding.py | 42 +- .../window/moments/test_moments_rolling.py | 403 ++++++++---------- 4 files changed, 242 insertions(+), 273 deletions(-) diff --git a/pandas/tests/window/common.py b/pandas/tests/window/common.py index a8bb9512b5dd4..e5de6355db026 100644 --- a/pandas/tests/window/common.py +++ b/pandas/tests/window/common.py @@ -25,7 +25,7 @@ def _create_data(self): # create the data only once as we are not setting it -def create_consistency_data(): +def _create_consistency_data(): def create_series(): return [ Series(dtype=object), @@ -167,8 +167,10 @@ def no_nans(x): # data is a tuple(object, is_constant, no_nans) data = create_series() + create_dataframes() - for x in data: - yield x, is_constant(x), no_nans(x) + return [(x, is_constant(x), no_nans(x)) for x in data] + + +consistency_data = _create_consistency_data() class ConsistencyBase(Base): @@ -252,7 +254,7 @@ def check_binary_ew_min_periods(name, min_periods, A, B): tm.assert_series_equal(result, Series([np.NaN])) -def test_moments_consistency_mock_mean(x, mean, mock_mean): +def moments_consistency_mock_mean(x, mean, mock_mean): mean_x = mean(x) # check that correlation of a series with itself is either 1 or NaN @@ -262,9 +264,7 @@ def test_moments_consistency_mock_mean(x, mean, mock_mean): tm.assert_equal(mean_x, expected.astype("float64")) -def test_moments_consistency_is_constant( - x, is_constant, min_periods, count, mean, corr -): +def moments_consistency_is_constant(x, is_constant, min_periods, count, mean, corr): count_x = count(x) mean_x = mean(x) # check that correlation of a series with itself is either 1 or NaN @@ -283,8 +283,8 @@ def test_moments_consistency_is_constant( tm.assert_equal(corr_x_x, expected) -def test_moments_consistency_var_debiasing_factors( - x, var_biased=None, var_unbiased=None, var_debiasing_factors=None +def moments_consistency_var_debiasing_factors( + x, var_biased, var_unbiased, var_debiasing_factors ): if var_unbiased and var_biased and var_debiasing_factors: # check variance debiasing factors @@ -294,7 +294,7 @@ def test_moments_consistency_var_debiasing_factors( tm.assert_equal(var_unbiased_x, var_biased_x * var_debiasing_factors_x) -def test_moments_consistency_var_data( +def moments_consistency_var_data( x, is_constant, min_periods, count, mean, var_unbiased, var_biased ): count_x = count(x) @@ -318,9 +318,7 @@ def test_moments_consistency_var_data( tm.assert_equal(var_x, expected) -def test_moments_consistency_std_data( - x, std_unbiased, var_unbiased, std_biased, var_biased -): +def moments_consistency_std_data(x, std_unbiased, var_unbiased, std_biased, var_biased): for (std, var) in [(std_biased, var_biased), (std_unbiased, var_unbiased)]: var_x = var(x) std_x = std(x) @@ -331,9 +329,7 @@ def test_moments_consistency_std_data( tm.assert_equal(var_x, std_x * std_x) -def test_moments_consistency_cov_data( - x, cov_unbiased, var_unbiased, cov_biased, var_biased -): +def moments_consistency_cov_data(x, cov_unbiased, var_unbiased, cov_biased, var_biased): for (cov, var) in [(cov_biased, var_biased), (cov_unbiased, var_unbiased)]: var_x = var(x) assert not (var_x < 0).any().any() @@ -345,7 +341,7 @@ def test_moments_consistency_cov_data( tm.assert_equal(var_x, cov_x_x) -def test_moments_consistency_series_data( +def moments_consistency_series_data( x, corr, mean, diff --git a/pandas/tests/window/moments/test_moments_ewm.py b/pandas/tests/window/moments/test_moments_ewm.py index 853120b96231b..b4653163f4911 100644 --- a/pandas/tests/window/moments/test_moments_ewm.py +++ b/pandas/tests/window/moments/test_moments_ewm.py @@ -10,15 +10,15 @@ ConsistencyBase, check_binary_ew, check_binary_ew_min_periods, - create_consistency_data, + consistency_data, ew_func, - test_moments_consistency_cov_data, - test_moments_consistency_is_constant, - test_moments_consistency_mock_mean, - test_moments_consistency_series_data, - test_moments_consistency_std_data, - test_moments_consistency_var_data, - test_moments_consistency_var_debiasing_factors, + moments_consistency_cov_data, + moments_consistency_is_constant, + moments_consistency_mock_mean, + moments_consistency_series_data, + moments_consistency_std_data, + moments_consistency_var_data, + moments_consistency_var_debiasing_factors, ) @@ -303,7 +303,7 @@ def test_different_input_array_raise_exception(self, name, binary_ew_data): @pytest.mark.slow -@pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) +@pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) @pytest.mark.parametrize("adjust", [True, False]) @pytest.mark.parametrize("ignore_na", [True, False]) @@ -366,7 +366,7 @@ def _ewma(s, com, min_periods, adjust, ignore_na): return result com = 3.0 - test_moments_consistency_mock_mean( + moments_consistency_mock_mean( x=x, mean=lambda x: x.ewm( com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na @@ -376,7 +376,7 @@ def _ewma(s, com, min_periods, adjust, ignore_na): ), ) - test_moments_consistency_is_constant( + moments_consistency_is_constant( x=x, is_constant=is_constant, min_periods=min_periods, @@ -389,7 +389,7 @@ def _ewma(s, com, min_periods, adjust, ignore_na): ).corr(y), ) - test_moments_consistency_var_debiasing_factors( + moments_consistency_var_debiasing_factors( x=x, var_unbiased=lambda x: ( x.ewm( @@ -407,13 +407,13 @@ def _ewma(s, com, min_periods, adjust, ignore_na): ) -@pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) +@pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) @pytest.mark.parametrize("adjust", [True, False]) @pytest.mark.parametrize("ignore_na", [True, False]) def test_ewm_consistency_var(x, is_constant, no_nans, min_periods, adjust, ignore_na): com = 3.0 - test_moments_consistency_var_data( + moments_consistency_var_data( x=x, is_constant=is_constant, min_periods=min_periods, @@ -434,13 +434,13 @@ def test_ewm_consistency_var(x, is_constant, no_nans, min_periods, adjust, ignor ) -@pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) +@pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) @pytest.mark.parametrize("adjust", [True, False]) @pytest.mark.parametrize("ignore_na", [True, False]) def test_ewm_consistency_std(x, is_constant, no_nans, min_periods, adjust, ignore_na): com = 3.0 - test_moments_consistency_std_data( + moments_consistency_std_data( x=x, var_unbiased=lambda x: ( x.ewm( @@ -463,13 +463,13 @@ def test_ewm_consistency_std(x, is_constant, no_nans, min_periods, adjust, ignor ) -@pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) +@pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) @pytest.mark.parametrize("adjust", [True, False]) @pytest.mark.parametrize("ignore_na", [True, False]) def test_ewm_consistency_cov(x, is_constant, no_nans, min_periods, adjust, ignore_na): com = 3.0 - test_moments_consistency_cov_data( + moments_consistency_cov_data( x=x, var_unbiased=lambda x: ( x.ewm( @@ -494,7 +494,7 @@ def test_ewm_consistency_cov(x, is_constant, no_nans, min_periods, adjust, ignor ) -@pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) +@pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) @pytest.mark.parametrize("adjust", [True, False]) @pytest.mark.parametrize("ignore_na", [True, False]) @@ -502,7 +502,7 @@ def test_ewm_consistency_series_data( x, is_constant, no_nans, min_periods, adjust, ignore_na ): com = 3.0 - test_moments_consistency_series_data( + moments_consistency_series_data( x=x, mean=lambda x: x.ewm( com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na diff --git a/pandas/tests/window/moments/test_moments_expanding.py b/pandas/tests/window/moments/test_moments_expanding.py index d8e4e3b98733a..f580d09a6a732 100644 --- a/pandas/tests/window/moments/test_moments_expanding.py +++ b/pandas/tests/window/moments/test_moments_expanding.py @@ -8,14 +8,14 @@ import pandas._testing as tm from pandas.tests.window.common import ( ConsistencyBase, - create_consistency_data, - test_moments_consistency_cov_data, - test_moments_consistency_is_constant, - test_moments_consistency_mock_mean, - test_moments_consistency_series_data, - test_moments_consistency_std_data, - test_moments_consistency_var_data, - test_moments_consistency_var_debiasing_factors, + consistency_data, + moments_consistency_cov_data, + moments_consistency_is_constant, + moments_consistency_mock_mean, + moments_consistency_series_data, + moments_consistency_std_data, + moments_consistency_var_data, + moments_consistency_var_debiasing_factors, ) @@ -343,7 +343,7 @@ def test_moment_functions_zero_length_pairwise(self, f): tm.assert_frame_equal(df2_result, df2_expected) @pytest.mark.slow - @pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) + @pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) def test_expanding_consistency(self, x, is_constant, no_nans, min_periods): @@ -357,14 +357,14 @@ def test_expanding_consistency(self, x, is_constant, no_nans, min_periods): ) # test consistency between different expanding_* moments - test_moments_consistency_mock_mean( + moments_consistency_mock_mean( x=x, mean=lambda x: x.expanding(min_periods=min_periods).mean(), mock_mean=lambda x: x.expanding(min_periods=min_periods).sum() / x.expanding().count(), ) - test_moments_consistency_is_constant( + moments_consistency_is_constant( x=x, is_constant=is_constant, min_periods=min_periods, @@ -373,7 +373,7 @@ def test_expanding_consistency(self, x, is_constant, no_nans, min_periods): corr=lambda x, y: x.expanding(min_periods=min_periods).corr(y), ) - test_moments_consistency_var_debiasing_factors( + moments_consistency_var_debiasing_factors( x=x, var_unbiased=lambda x: x.expanding(min_periods=min_periods).var(), var_biased=lambda x: x.expanding(min_periods=min_periods).var(ddof=0), @@ -383,7 +383,7 @@ def test_expanding_consistency(self, x, is_constant, no_nans, min_periods): ), ) - @pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) + @pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) def test_expanding_apply_consistency(self, x, is_constant, no_nans, min_periods): with warnings.catch_warnings(): @@ -429,10 +429,10 @@ def test_expanding_apply_consistency(self, x, is_constant, no_nans, min_periods) tm.assert_equal(expanding_f_result, expanding_apply_f_result) -@pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) +@pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) def test_moments_consistency_var(x, is_constant, no_nans, min_periods): - test_moments_consistency_var_data( + moments_consistency_var_data( x=x, is_constant=is_constant, min_periods=min_periods, @@ -443,10 +443,10 @@ def test_moments_consistency_var(x, is_constant, no_nans, min_periods): ) -@pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) +@pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) def test_expanding_consistency_std(x, is_constant, no_nans, min_periods): - test_moments_consistency_std_data( + moments_consistency_std_data( x=x, var_unbiased=lambda x: x.expanding(min_periods=min_periods).var(), std_unbiased=lambda x: x.expanding(min_periods=min_periods).std(), @@ -455,10 +455,10 @@ def test_expanding_consistency_std(x, is_constant, no_nans, min_periods): ) -@pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) +@pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) def test_expanding_consistency_cov(x, is_constant, no_nans, min_periods): - test_moments_consistency_cov_data( + moments_consistency_cov_data( x=x, var_unbiased=lambda x: x.expanding(min_periods=min_periods).var(), cov_unbiased=lambda x, y: x.expanding(min_periods=min_periods).cov(y), @@ -467,10 +467,10 @@ def test_expanding_consistency_cov(x, is_constant, no_nans, min_periods): ) -@pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) +@pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) def test_expanding_consistency_series(x, is_constant, no_nans, min_periods): - test_moments_consistency_series_data( + moments_consistency_series_data( x=x, mean=lambda x: x.expanding(min_periods=min_periods).mean(), corr=lambda x, y: x.expanding(min_periods=min_periods).corr(y), diff --git a/pandas/tests/window/moments/test_moments_rolling.py b/pandas/tests/window/moments/test_moments_rolling.py index ced5f9f0d54f8..fc16f43180927 100644 --- a/pandas/tests/window/moments/test_moments_rolling.py +++ b/pandas/tests/window/moments/test_moments_rolling.py @@ -15,14 +15,14 @@ from pandas.tests.window.common import ( Base, ConsistencyBase, - create_consistency_data, - test_moments_consistency_cov_data, - test_moments_consistency_is_constant, - test_moments_consistency_mock_mean, - test_moments_consistency_series_data, - test_moments_consistency_std_data, - test_moments_consistency_var_data, - test_moments_consistency_var_debiasing_factors, + consistency_data, + moments_consistency_cov_data, + moments_consistency_is_constant, + moments_consistency_mock_mean, + moments_consistency_series_data, + moments_consistency_std_data, + moments_consistency_var_data, + moments_consistency_var_debiasing_factors, ) import pandas.tseries.offsets as offsets @@ -947,91 +947,7 @@ class TestRollingMomentsConsistency(ConsistencyBase): def setup_method(self, method): self._create_data() - @pytest.mark.slow - @pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) - @pytest.mark.parametrize( - "window,min_periods,center", list(_rolling_consistency_cases()) - ) - def test_rolling_consistency( - self, x, is_constant, no_nans, window, min_periods, center - ): - - # suppress warnings about empty slices, as we are deliberately testing - # with empty/0-length Series/DataFrames - with warnings.catch_warnings(): - warnings.filterwarnings( - "ignore", - message=".*(empty slice|0 for slice).*", - category=RuntimeWarning, - ) - - # test consistency between different rolling_* moments - test_moments_consistency_mock_mean( - x=x, - mean=lambda x: ( - x.rolling( - window=window, min_periods=min_periods, center=center - ).mean() - ), - mock_mean=lambda x: ( - x.rolling(window=window, min_periods=min_periods, center=center) - .sum() - .divide( - x.rolling( - window=window, min_periods=min_periods, center=center - ).count() - ) - ), - ) - - test_moments_consistency_is_constant( - x=x, - is_constant=is_constant, - min_periods=min_periods, - count=lambda x: ( - x.rolling( - window=window, min_periods=min_periods, center=center - ).count() - ), - mean=lambda x: ( - x.rolling( - window=window, min_periods=min_periods, center=center - ).mean() - ), - corr=lambda x, y: ( - x.rolling( - window=window, min_periods=min_periods, center=center - ).corr(y) - ), - ) - - test_moments_consistency_var_debiasing_factors( - x=x, - var_unbiased=lambda x: ( - x.rolling( - window=window, min_periods=min_periods, center=center - ).var() - ), - var_biased=lambda x: ( - x.rolling( - window=window, min_periods=min_periods, center=center - ).var(ddof=0) - ), - var_debiasing_factors=lambda x: ( - x.rolling(window=window, min_periods=min_periods, center=center) - .count() - .divide( - ( - x.rolling( - window=window, min_periods=min_periods, center=center - ).count() - - 1.0 - ).replace(0.0, np.nan) - ) - ), - ) - - @pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) + @pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) @pytest.mark.parametrize( "window,min_periods,center", list(_rolling_consistency_cases()) ) @@ -1084,128 +1000,6 @@ def test_rolling_apply_consistency( if name in ["sum", "prod"]: tm.assert_equal(rolling_f_result, rolling_apply_f_result) - @pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) - @pytest.mark.parametrize( - "window,min_periods,center", list(_rolling_consistency_cases()) - ) - def test_rolling_consistency_var( - self, x, is_constant, no_nans, window, min_periods, center - ): - test_moments_consistency_var_data( - x=x, - is_constant=is_constant, - min_periods=min_periods, - count=lambda x: ( - x.rolling(window=window, min_periods=min_periods, center=center).count() - ), - mean=lambda x: ( - x.rolling(window=window, min_periods=min_periods, center=center).mean() - ), - var_unbiased=lambda x: ( - x.rolling(window=window, min_periods=min_periods, center=center).var() - ), - var_biased=lambda x: ( - x.rolling(window=window, min_periods=min_periods, center=center).var( - ddof=0 - ) - ), - ) - - @pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) - @pytest.mark.parametrize( - "window,min_periods,center", list(_rolling_consistency_cases()) - ) - def test_rolling_consistency_std( - self, x, is_constant, no_nans, window, min_periods, center - ): - test_moments_consistency_std_data( - x=x, - var_unbiased=lambda x: ( - x.rolling(window=window, min_periods=min_periods, center=center).var() - ), - std_unbiased=lambda x: ( - x.rolling(window=window, min_periods=min_periods, center=center).std() - ), - var_biased=lambda x: ( - x.rolling(window=window, min_periods=min_periods, center=center).var( - ddof=0 - ) - ), - std_biased=lambda x: ( - x.rolling(window=window, min_periods=min_periods, center=center).std( - ddof=0 - ) - ), - ) - - @pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) - @pytest.mark.parametrize( - "window,min_periods,center", list(_rolling_consistency_cases()) - ) - def test_rolling_consistency_cov( - self, x, is_constant, no_nans, window, min_periods, center - ): - test_moments_consistency_cov_data( - x=x, - var_unbiased=lambda x: ( - x.rolling(window=window, min_periods=min_periods, center=center).var() - ), - cov_unbiased=lambda x, y: ( - x.rolling(window=window, min_periods=min_periods, center=center).cov(y) - ), - var_biased=lambda x: ( - x.rolling(window=window, min_periods=min_periods, center=center).var( - ddof=0 - ) - ), - cov_biased=lambda x, y: ( - x.rolling(window=window, min_periods=min_periods, center=center).cov( - y, ddof=0 - ) - ), - ) - - @pytest.mark.parametrize("x, is_constant, no_nans", list(create_consistency_data())) - @pytest.mark.parametrize( - "window,min_periods,center", list(_rolling_consistency_cases()) - ) - def test_rolling_consistency_series( - self, x, is_constant, no_nans, window, min_periods, center - ): - test_moments_consistency_series_data( - x=x, - mean=lambda x: ( - x.rolling(window=window, min_periods=min_periods, center=center).mean() - ), - corr=lambda x, y: ( - x.rolling(window=window, min_periods=min_periods, center=center).corr(y) - ), - var_unbiased=lambda x: ( - x.rolling(window=window, min_periods=min_periods, center=center).var() - ), - std_unbiased=lambda x: ( - x.rolling(window=window, min_periods=min_periods, center=center).std() - ), - cov_unbiased=lambda x, y: ( - x.rolling(window=window, min_periods=min_periods, center=center).cov(y) - ), - var_biased=lambda x: ( - x.rolling(window=window, min_periods=min_periods, center=center).var( - ddof=0 - ) - ), - std_biased=lambda x: ( - x.rolling(window=window, min_periods=min_periods, center=center).std( - ddof=0 - ) - ), - cov_biased=lambda x, y: ( - x.rolling(window=window, min_periods=min_periods, center=center).cov( - y, ddof=0 - ) - ), - ) - # binary moments def test_rolling_cov(self): A = self.series @@ -1636,3 +1430,182 @@ def test_moment_functions_zero_length_pairwise(self): df2_result = f(df2) tm.assert_frame_equal(df2_result, df2_expected) + + +@pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) +@pytest.mark.parametrize( + "window,min_periods,center", list(_rolling_consistency_cases()) +) +def test_rolling_consistency_var(x, is_constant, no_nans, window, min_periods, center): + moments_consistency_var_data( + x=x, + is_constant=is_constant, + min_periods=min_periods, + count=lambda x: ( + x.rolling(window=window, min_periods=min_periods, center=center).count() + ), + mean=lambda x: ( + x.rolling(window=window, min_periods=min_periods, center=center).mean() + ), + var_unbiased=lambda x: ( + x.rolling(window=window, min_periods=min_periods, center=center).var() + ), + var_biased=lambda x: ( + x.rolling(window=window, min_periods=min_periods, center=center).var(ddof=0) + ), + ) + + +@pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) +@pytest.mark.parametrize( + "window,min_periods,center", list(_rolling_consistency_cases()) +) +def test_rolling_consistency_std(x, is_constant, no_nans, window, min_periods, center): + moments_consistency_std_data( + x=x, + var_unbiased=lambda x: ( + x.rolling(window=window, min_periods=min_periods, center=center).var() + ), + std_unbiased=lambda x: ( + x.rolling(window=window, min_periods=min_periods, center=center).std() + ), + var_biased=lambda x: ( + x.rolling(window=window, min_periods=min_periods, center=center).var(ddof=0) + ), + std_biased=lambda x: ( + x.rolling(window=window, min_periods=min_periods, center=center).std(ddof=0) + ), + ) + + +@pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) +@pytest.mark.parametrize( + "window,min_periods,center", list(_rolling_consistency_cases()) +) +def test_rolling_consistency_cov(x, is_constant, no_nans, window, min_periods, center): + moments_consistency_cov_data( + x=x, + var_unbiased=lambda x: ( + x.rolling(window=window, min_periods=min_periods, center=center).var() + ), + cov_unbiased=lambda x, y: ( + x.rolling(window=window, min_periods=min_periods, center=center).cov(y) + ), + var_biased=lambda x: ( + x.rolling(window=window, min_periods=min_periods, center=center).var(ddof=0) + ), + cov_biased=lambda x, y: ( + x.rolling(window=window, min_periods=min_periods, center=center).cov( + y, ddof=0 + ) + ), + ) + + +@pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) +@pytest.mark.parametrize( + "window,min_periods,center", list(_rolling_consistency_cases()) +) +def test_rolling_consistency_series( + x, is_constant, no_nans, window, min_periods, center +): + moments_consistency_series_data( + x=x, + mean=lambda x: ( + x.rolling(window=window, min_periods=min_periods, center=center).mean() + ), + corr=lambda x, y: ( + x.rolling(window=window, min_periods=min_periods, center=center).corr(y) + ), + var_unbiased=lambda x: ( + x.rolling(window=window, min_periods=min_periods, center=center).var() + ), + std_unbiased=lambda x: ( + x.rolling(window=window, min_periods=min_periods, center=center).std() + ), + cov_unbiased=lambda x, y: ( + x.rolling(window=window, min_periods=min_periods, center=center).cov(y) + ), + var_biased=lambda x: ( + x.rolling(window=window, min_periods=min_periods, center=center).var(ddof=0) + ), + std_biased=lambda x: ( + x.rolling(window=window, min_periods=min_periods, center=center).std(ddof=0) + ), + cov_biased=lambda x, y: ( + x.rolling(window=window, min_periods=min_periods, center=center).cov( + y, ddof=0 + ) + ), + ) + + +@pytest.mark.slow +@pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) +@pytest.mark.parametrize( + "window,min_periods,center", list(_rolling_consistency_cases()) +) +def test_rolling_consistency(x, is_constant, no_nans, window, min_periods, center): + + # suppress warnings about empty slices, as we are deliberately testing + # with empty/0-length Series/DataFrames + with warnings.catch_warnings(): + warnings.filterwarnings( + "ignore", message=".*(empty slice|0 for slice).*", category=RuntimeWarning, + ) + + # test consistency between different rolling_* moments + moments_consistency_mock_mean( + x=x, + mean=lambda x: ( + x.rolling(window=window, min_periods=min_periods, center=center).mean() + ), + mock_mean=lambda x: ( + x.rolling(window=window, min_periods=min_periods, center=center) + .sum() + .divide( + x.rolling( + window=window, min_periods=min_periods, center=center + ).count() + ) + ), + ) + + moments_consistency_is_constant( + x=x, + is_constant=is_constant, + min_periods=min_periods, + count=lambda x: ( + x.rolling(window=window, min_periods=min_periods, center=center).count() + ), + mean=lambda x: ( + x.rolling(window=window, min_periods=min_periods, center=center).mean() + ), + corr=lambda x, y: ( + x.rolling(window=window, min_periods=min_periods, center=center).corr(y) + ), + ) + + moments_consistency_var_debiasing_factors( + x=x, + var_unbiased=lambda x: ( + x.rolling(window=window, min_periods=min_periods, center=center).var() + ), + var_biased=lambda x: ( + x.rolling(window=window, min_periods=min_periods, center=center).var( + ddof=0 + ) + ), + var_debiasing_factors=lambda x: ( + x.rolling(window=window, min_periods=min_periods, center=center) + .count() + .divide( + ( + x.rolling( + window=window, min_periods=min_periods, center=center + ).count() + - 1.0 + ).replace(0.0, np.nan) + ) + ), + ) From 8be18a730d38e5c25fb85289b90f985c6ac4eb69 Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Wed, 29 Apr 2020 21:17:13 +0200 Subject: [PATCH 8/9] fixturize consistency data --- pandas/tests/window/common.py | 151 +---------------- pandas/tests/window/conftest.py | 155 ++++++++++++++++++ .../tests/window/moments/test_moments_ewm.py | 23 ++- .../window/moments/test_moments_expanding.py | 26 ++- .../window/moments/test_moments_rolling.py | 29 ++-- 5 files changed, 190 insertions(+), 194 deletions(-) diff --git a/pandas/tests/window/common.py b/pandas/tests/window/common.py index e5de6355db026..7a7ab57cdb904 100644 --- a/pandas/tests/window/common.py +++ b/pandas/tests/window/common.py @@ -3,7 +3,7 @@ import numpy as np from numpy.random import randn -from pandas import DataFrame, Series, bdate_range, notna +from pandas import DataFrame, Series, bdate_range import pandas._testing as tm N, K = 100, 10 @@ -24,155 +24,6 @@ def _create_data(self): self.frame = DataFrame(randn(N, K), index=self.rng, columns=np.arange(K)) -# create the data only once as we are not setting it -def _create_consistency_data(): - def create_series(): - return [ - Series(dtype=object), - Series([np.nan]), - Series([np.nan, np.nan]), - Series([3.0]), - Series([np.nan, 3.0]), - Series([3.0, np.nan]), - Series([1.0, 3.0]), - Series([2.0, 2.0]), - Series([3.0, 1.0]), - Series( - [5.0, 5.0, 5.0, 5.0, np.nan, np.nan, np.nan, 5.0, 5.0, np.nan, np.nan] - ), - Series( - [ - np.nan, - 5.0, - 5.0, - 5.0, - np.nan, - np.nan, - np.nan, - 5.0, - 5.0, - np.nan, - np.nan, - ] - ), - Series( - [ - np.nan, - np.nan, - 5.0, - 5.0, - np.nan, - np.nan, - np.nan, - 5.0, - 5.0, - np.nan, - np.nan, - ] - ), - Series( - [ - np.nan, - 3.0, - np.nan, - 3.0, - 4.0, - 5.0, - 6.0, - np.nan, - np.nan, - 7.0, - 12.0, - 13.0, - 14.0, - 15.0, - ] - ), - Series( - [ - np.nan, - 5.0, - np.nan, - 2.0, - 4.0, - 0.0, - 9.0, - np.nan, - np.nan, - 3.0, - 12.0, - 13.0, - 14.0, - 15.0, - ] - ), - Series( - [ - 2.0, - 3.0, - np.nan, - 3.0, - 4.0, - 5.0, - 6.0, - np.nan, - np.nan, - 7.0, - 12.0, - 13.0, - 14.0, - 15.0, - ] - ), - Series( - [ - 2.0, - 5.0, - np.nan, - 2.0, - 4.0, - 0.0, - 9.0, - np.nan, - np.nan, - 3.0, - 12.0, - 13.0, - 14.0, - 15.0, - ] - ), - Series(range(10)), - Series(range(20, 0, -2)), - ] - - def create_dataframes(): - return [ - DataFrame(), - DataFrame(columns=["a"]), - DataFrame(columns=["a", "a"]), - DataFrame(columns=["a", "b"]), - DataFrame(np.arange(10).reshape((5, 2))), - DataFrame(np.arange(25).reshape((5, 5))), - DataFrame(np.arange(25).reshape((5, 5)), columns=["a", "b", 99, "d", "d"]), - ] + [DataFrame(s) for s in create_series()] - - def is_constant(x): - values = x.values.ravel() - return len(set(values[notna(values)])) == 1 - - def no_nans(x): - return x.notna().all().all() - - # data is a tuple(object, is_constant, no_nans) - data = create_series() + create_dataframes() - - return [(x, is_constant(x), no_nans(x)) for x in data] - - -consistency_data = _create_consistency_data() - - class ConsistencyBase(Base): base_functions = [ (lambda v: Series(v).count(), None, "count"), diff --git a/pandas/tests/window/conftest.py b/pandas/tests/window/conftest.py index fb46ca51ace58..856c8f3884816 100644 --- a/pandas/tests/window/conftest.py +++ b/pandas/tests/window/conftest.py @@ -1,7 +1,10 @@ +import numpy as np import pytest import pandas.util._test_decorators as td +from pandas import DataFrame, Series, notna + @pytest.fixture(params=[True, False]) def raw(request): @@ -87,3 +90,155 @@ def engine(request): def engine_and_raw(request): """engine and raw keyword arguments for rolling.apply""" return request.param + + +# create the data only once as we are not setting it +def _create_consistency_data(): + def create_series(): + return [ + Series(dtype=object), + Series([np.nan]), + Series([np.nan, np.nan]), + Series([3.0]), + Series([np.nan, 3.0]), + Series([3.0, np.nan]), + Series([1.0, 3.0]), + Series([2.0, 2.0]), + Series([3.0, 1.0]), + Series( + [5.0, 5.0, 5.0, 5.0, np.nan, np.nan, np.nan, 5.0, 5.0, np.nan, np.nan] + ), + Series( + [ + np.nan, + 5.0, + 5.0, + 5.0, + np.nan, + np.nan, + np.nan, + 5.0, + 5.0, + np.nan, + np.nan, + ] + ), + Series( + [ + np.nan, + np.nan, + 5.0, + 5.0, + np.nan, + np.nan, + np.nan, + 5.0, + 5.0, + np.nan, + np.nan, + ] + ), + Series( + [ + np.nan, + 3.0, + np.nan, + 3.0, + 4.0, + 5.0, + 6.0, + np.nan, + np.nan, + 7.0, + 12.0, + 13.0, + 14.0, + 15.0, + ] + ), + Series( + [ + np.nan, + 5.0, + np.nan, + 2.0, + 4.0, + 0.0, + 9.0, + np.nan, + np.nan, + 3.0, + 12.0, + 13.0, + 14.0, + 15.0, + ] + ), + Series( + [ + 2.0, + 3.0, + np.nan, + 3.0, + 4.0, + 5.0, + 6.0, + np.nan, + np.nan, + 7.0, + 12.0, + 13.0, + 14.0, + 15.0, + ] + ), + Series( + [ + 2.0, + 5.0, + np.nan, + 2.0, + 4.0, + 0.0, + 9.0, + np.nan, + np.nan, + 3.0, + 12.0, + 13.0, + 14.0, + 15.0, + ] + ), + Series(range(10)), + Series(range(20, 0, -2)), + ] + + def create_dataframes(): + return [ + DataFrame(), + DataFrame(columns=["a"]), + DataFrame(columns=["a", "a"]), + DataFrame(columns=["a", "b"]), + DataFrame(np.arange(10).reshape((5, 2))), + DataFrame(np.arange(25).reshape((5, 5))), + DataFrame(np.arange(25).reshape((5, 5)), columns=["a", "b", 99, "d", "d"]), + ] + [DataFrame(s) for s in create_series()] + + def is_constant(x): + values = x.values.ravel() + return len(set(values[notna(values)])) == 1 + + def no_nans(x): + return x.notna().all().all() + + # data is a tuple(object, is_constant, no_nans) + data = create_series() + create_dataframes() + + return [(x, is_constant(x), no_nans(x)) for x in data] + + +@pytest.fixture(params=_create_consistency_data()) +def consistency_data(request): + """Create consistency data""" + return request.param diff --git a/pandas/tests/window/moments/test_moments_ewm.py b/pandas/tests/window/moments/test_moments_ewm.py index b4653163f4911..e5fd38d30c2df 100644 --- a/pandas/tests/window/moments/test_moments_ewm.py +++ b/pandas/tests/window/moments/test_moments_ewm.py @@ -10,7 +10,6 @@ ConsistencyBase, check_binary_ew, check_binary_ew_min_periods, - consistency_data, ew_func, moments_consistency_cov_data, moments_consistency_is_constant, @@ -303,11 +302,10 @@ def test_different_input_array_raise_exception(self, name, binary_ew_data): @pytest.mark.slow -@pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) @pytest.mark.parametrize("adjust", [True, False]) @pytest.mark.parametrize("ignore_na", [True, False]) -def test_ewm_consistency(x, is_constant, no_nans, min_periods, adjust, ignore_na): +def test_ewm_consistency(consistency_data, min_periods, adjust, ignore_na): def _weights(s, com, adjust, ignore_na): if isinstance(s, DataFrame): if not len(s.columns): @@ -365,6 +363,7 @@ def _ewma(s, com, min_periods, adjust, ignore_na): ] = np.nan return result + x, is_constant, no_nans = consistency_data com = 3.0 moments_consistency_mock_mean( x=x, @@ -407,11 +406,11 @@ def _ewma(s, com, min_periods, adjust, ignore_na): ) -@pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) @pytest.mark.parametrize("adjust", [True, False]) @pytest.mark.parametrize("ignore_na", [True, False]) -def test_ewm_consistency_var(x, is_constant, no_nans, min_periods, adjust, ignore_na): +def test_ewm_consistency_var(consistency_data, min_periods, adjust, ignore_na): + x, is_constant, no_nans = consistency_data com = 3.0 moments_consistency_var_data( x=x, @@ -434,11 +433,11 @@ def test_ewm_consistency_var(x, is_constant, no_nans, min_periods, adjust, ignor ) -@pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) @pytest.mark.parametrize("adjust", [True, False]) @pytest.mark.parametrize("ignore_na", [True, False]) -def test_ewm_consistency_std(x, is_constant, no_nans, min_periods, adjust, ignore_na): +def test_ewm_consistency_std(consistency_data, min_periods, adjust, ignore_na): + x, is_constant, no_nans = consistency_data com = 3.0 moments_consistency_std_data( x=x, @@ -463,11 +462,11 @@ def test_ewm_consistency_std(x, is_constant, no_nans, min_periods, adjust, ignor ) -@pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) @pytest.mark.parametrize("adjust", [True, False]) @pytest.mark.parametrize("ignore_na", [True, False]) -def test_ewm_consistency_cov(x, is_constant, no_nans, min_periods, adjust, ignore_na): +def test_ewm_consistency_cov(consistency_data, min_periods, adjust, ignore_na): + x, is_constant, no_nans = consistency_data com = 3.0 moments_consistency_cov_data( x=x, @@ -494,13 +493,11 @@ def test_ewm_consistency_cov(x, is_constant, no_nans, min_periods, adjust, ignor ) -@pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) @pytest.mark.parametrize("adjust", [True, False]) @pytest.mark.parametrize("ignore_na", [True, False]) -def test_ewm_consistency_series_data( - x, is_constant, no_nans, min_periods, adjust, ignore_na -): +def test_ewm_consistency_series_data(consistency_data, min_periods, adjust, ignore_na): + x, is_constant, no_nans = consistency_data com = 3.0 moments_consistency_series_data( x=x, diff --git a/pandas/tests/window/moments/test_moments_expanding.py b/pandas/tests/window/moments/test_moments_expanding.py index f580d09a6a732..e97383823f00d 100644 --- a/pandas/tests/window/moments/test_moments_expanding.py +++ b/pandas/tests/window/moments/test_moments_expanding.py @@ -8,7 +8,6 @@ import pandas._testing as tm from pandas.tests.window.common import ( ConsistencyBase, - consistency_data, moments_consistency_cov_data, moments_consistency_is_constant, moments_consistency_mock_mean, @@ -343,10 +342,9 @@ def test_moment_functions_zero_length_pairwise(self, f): tm.assert_frame_equal(df2_result, df2_expected) @pytest.mark.slow - @pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) - def test_expanding_consistency(self, x, is_constant, no_nans, min_periods): - + def test_expanding_consistency(self, consistency_data, min_periods): + x, is_constant, no_nans = consistency_data # suppress warnings about empty slices, as we are deliberately testing # with empty/0-length Series/DataFrames with warnings.catch_warnings(): @@ -383,9 +381,9 @@ def test_expanding_consistency(self, x, is_constant, no_nans, min_periods): ), ) - @pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) - def test_expanding_apply_consistency(self, x, is_constant, no_nans, min_periods): + def test_expanding_apply_consistency(self, consistency_data, min_periods): + x, is_constant, no_nans = consistency_data with warnings.catch_warnings(): warnings.filterwarnings( "ignore", @@ -429,9 +427,9 @@ def test_expanding_apply_consistency(self, x, is_constant, no_nans, min_periods) tm.assert_equal(expanding_f_result, expanding_apply_f_result) -@pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) -def test_moments_consistency_var(x, is_constant, no_nans, min_periods): +def test_moments_consistency_var(consistency_data, min_periods): + x, is_constant, no_nans = consistency_data moments_consistency_var_data( x=x, is_constant=is_constant, @@ -443,9 +441,9 @@ def test_moments_consistency_var(x, is_constant, no_nans, min_periods): ) -@pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) -def test_expanding_consistency_std(x, is_constant, no_nans, min_periods): +def test_expanding_consistency_std(consistency_data, min_periods): + x, is_constant, no_nans = consistency_data moments_consistency_std_data( x=x, var_unbiased=lambda x: x.expanding(min_periods=min_periods).var(), @@ -455,9 +453,9 @@ def test_expanding_consistency_std(x, is_constant, no_nans, min_periods): ) -@pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) -def test_expanding_consistency_cov(x, is_constant, no_nans, min_periods): +def test_expanding_consistency_cov(consistency_data, min_periods): + x, is_constant, no_nans = consistency_data moments_consistency_cov_data( x=x, var_unbiased=lambda x: x.expanding(min_periods=min_periods).var(), @@ -467,9 +465,9 @@ def test_expanding_consistency_cov(x, is_constant, no_nans, min_periods): ) -@pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) -def test_expanding_consistency_series(x, is_constant, no_nans, min_periods): +def test_expanding_consistency_series(consistency_data, min_periods): + x, is_constant, no_nans = consistency_data moments_consistency_series_data( x=x, mean=lambda x: x.expanding(min_periods=min_periods).mean(), diff --git a/pandas/tests/window/moments/test_moments_rolling.py b/pandas/tests/window/moments/test_moments_rolling.py index fc16f43180927..92c40e19c46b8 100644 --- a/pandas/tests/window/moments/test_moments_rolling.py +++ b/pandas/tests/window/moments/test_moments_rolling.py @@ -15,7 +15,6 @@ from pandas.tests.window.common import ( Base, ConsistencyBase, - consistency_data, moments_consistency_cov_data, moments_consistency_is_constant, moments_consistency_mock_mean, @@ -947,14 +946,13 @@ class TestRollingMomentsConsistency(ConsistencyBase): def setup_method(self, method): self._create_data() - @pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) @pytest.mark.parametrize( "window,min_periods,center", list(_rolling_consistency_cases()) ) def test_rolling_apply_consistency( - self, x, is_constant, no_nans, window, min_periods, center + self, consistency_data, window, min_periods, center ): - + x, is_constant, no_nans = consistency_data with warnings.catch_warnings(): warnings.filterwarnings( "ignore", @@ -1432,11 +1430,11 @@ def test_moment_functions_zero_length_pairwise(self): tm.assert_frame_equal(df2_result, df2_expected) -@pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) @pytest.mark.parametrize( "window,min_periods,center", list(_rolling_consistency_cases()) ) -def test_rolling_consistency_var(x, is_constant, no_nans, window, min_periods, center): +def test_rolling_consistency_var(consistency_data, window, min_periods, center): + x, is_constant, no_nans = consistency_data moments_consistency_var_data( x=x, is_constant=is_constant, @@ -1456,11 +1454,11 @@ def test_rolling_consistency_var(x, is_constant, no_nans, window, min_periods, c ) -@pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) @pytest.mark.parametrize( "window,min_periods,center", list(_rolling_consistency_cases()) ) -def test_rolling_consistency_std(x, is_constant, no_nans, window, min_periods, center): +def test_rolling_consistency_std(consistency_data, window, min_periods, center): + x, is_constant, no_nans = consistency_data moments_consistency_std_data( x=x, var_unbiased=lambda x: ( @@ -1478,11 +1476,11 @@ def test_rolling_consistency_std(x, is_constant, no_nans, window, min_periods, c ) -@pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) @pytest.mark.parametrize( "window,min_periods,center", list(_rolling_consistency_cases()) ) -def test_rolling_consistency_cov(x, is_constant, no_nans, window, min_periods, center): +def test_rolling_consistency_cov(consistency_data, window, min_periods, center): + x, is_constant, no_nans = consistency_data moments_consistency_cov_data( x=x, var_unbiased=lambda x: ( @@ -1502,13 +1500,11 @@ def test_rolling_consistency_cov(x, is_constant, no_nans, window, min_periods, c ) -@pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) @pytest.mark.parametrize( "window,min_periods,center", list(_rolling_consistency_cases()) ) -def test_rolling_consistency_series( - x, is_constant, no_nans, window, min_periods, center -): +def test_rolling_consistency_series(consistency_data, window, min_periods, center): + x, is_constant, no_nans = consistency_data moments_consistency_series_data( x=x, mean=lambda x: ( @@ -1541,12 +1537,11 @@ def test_rolling_consistency_series( @pytest.mark.slow -@pytest.mark.parametrize("x, is_constant, no_nans", consistency_data) @pytest.mark.parametrize( "window,min_periods,center", list(_rolling_consistency_cases()) ) -def test_rolling_consistency(x, is_constant, no_nans, window, min_periods, center): - +def test_rolling_consistency(consistency_data, window, min_periods, center): + x, is_constant, no_nans = consistency_data # suppress warnings about empty slices, as we are deliberately testing # with empty/0-length Series/DataFrames with warnings.catch_warnings(): From 3ebe4d9e4ae74be71220b3ef1c4574b6df74bc02 Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Thu, 30 Apr 2020 09:54:07 +0200 Subject: [PATCH 9/9] mark slow --- pandas/tests/window/moments/test_moments_ewm.py | 1 + pandas/tests/window/moments/test_moments_rolling.py | 1 + 2 files changed, 2 insertions(+) diff --git a/pandas/tests/window/moments/test_moments_ewm.py b/pandas/tests/window/moments/test_moments_ewm.py index e5fd38d30c2df..37048265253f7 100644 --- a/pandas/tests/window/moments/test_moments_ewm.py +++ b/pandas/tests/window/moments/test_moments_ewm.py @@ -493,6 +493,7 @@ def test_ewm_consistency_cov(consistency_data, min_periods, adjust, ignore_na): ) +@pytest.mark.slow @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) @pytest.mark.parametrize("adjust", [True, False]) @pytest.mark.parametrize("ignore_na", [True, False]) diff --git a/pandas/tests/window/moments/test_moments_rolling.py b/pandas/tests/window/moments/test_moments_rolling.py index 92c40e19c46b8..c15b7ed00b9e3 100644 --- a/pandas/tests/window/moments/test_moments_rolling.py +++ b/pandas/tests/window/moments/test_moments_rolling.py @@ -1500,6 +1500,7 @@ def test_rolling_consistency_cov(consistency_data, window, min_periods, center): ) +@pytest.mark.slow @pytest.mark.parametrize( "window,min_periods,center", list(_rolling_consistency_cases()) )