From edc8b335c9bccd9f88bd7d5f40fb206d091ebb5f Mon Sep 17 00:00:00 2001 From: StevenSchaerer <53116297+stevenschaerer@users.noreply.github.com> Date: Fri, 2 Feb 2024 22:52:17 +0100 Subject: [PATCH] BUG: Raise if an aggregation function other than mean is used with ewm (#51695) --- doc/source/whatsnew/v3.0.0.rst | 2 +- pandas/core/window/ewm.py | 12 ++++++++++++ pandas/tests/window/test_ewm.py | 12 ++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index f9117253b61c1..7c74a2698093d 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -205,7 +205,7 @@ Plotting Groupby/resample/rolling ^^^^^^^^^^^^^^^^^^^^^^^^ - Bug in :meth:`.DataFrameGroupBy.quantile` when ``interpolation="nearest"`` is inconsistent with :meth:`DataFrame.quantile` (:issue:`47942`) -- +- Bug in :meth:`DataFrame.ewm` and :meth:`Series.ewm` when passed ``times`` and aggregation functions other than mean (:issue:`51695`) Reshaping ^^^^^^^^^ diff --git a/pandas/core/window/ewm.py b/pandas/core/window/ewm.py index 3c07fc156aea1..01d1787d46ca0 100644 --- a/pandas/core/window/ewm.py +++ b/pandas/core/window/ewm.py @@ -587,6 +587,8 @@ def sum( ): if not self.adjust: raise NotImplementedError("sum is not implemented with adjust=False") + if self.times is not None: + raise NotImplementedError("sum is not implemented with times") if maybe_use_numba(engine): if self.method == "single": func = generate_numba_ewm_func @@ -658,6 +660,8 @@ def std(self, bias: bool = False, numeric_only: bool = False): raise NotImplementedError( f"{type(self).__name__}.std does not implement numeric_only" ) + if self.times is not None: + raise NotImplementedError("std is not implemented with times") return zsqrt(self.var(bias=bias, numeric_only=numeric_only)) @doc( @@ -691,6 +695,8 @@ def std(self, bias: bool = False, numeric_only: bool = False): agg_method="var", ) def var(self, bias: bool = False, numeric_only: bool = False): + if self.times is not None: + raise NotImplementedError("var is not implemented with times") window_func = window_aggregations.ewmcov wfunc = partial( window_func, @@ -753,6 +759,9 @@ def cov( bias: bool = False, numeric_only: bool = False, ): + if self.times is not None: + raise NotImplementedError("cov is not implemented with times") + from pandas import Series self._validate_numeric_only("cov", numeric_only) @@ -837,6 +846,9 @@ def corr( pairwise: bool | None = None, numeric_only: bool = False, ): + if self.times is not None: + raise NotImplementedError("corr is not implemented with times") + from pandas import Series self._validate_numeric_only("corr", numeric_only) diff --git a/pandas/tests/window/test_ewm.py b/pandas/tests/window/test_ewm.py index 2e2cfa156019f..c512b8ec6d868 100644 --- a/pandas/tests/window/test_ewm.py +++ b/pandas/tests/window/test_ewm.py @@ -173,6 +173,18 @@ def test_ewm_sum_adjust_false_notimplemented(): data.sum() +@pytest.mark.parametrize("method", ["sum", "std", "var", "cov", "corr"]) +def test_ewma_times_only_mean_implemented(frame_or_series, method): + # GH 51695 + halflife = "1 day" + times = date_range("2000", freq="D", periods=10) + ewm = frame_or_series(range(10)).ewm(halflife=halflife, times=times) + with pytest.raises( + NotImplementedError, match=f"{method} is not implemented with times" + ): + getattr(ewm, method)() + + @pytest.mark.parametrize( "expected_data, ignore", [[[10.0, 5.0, 2.5, 11.25], False], [[10.0, 5.0, 5.0, 12.5], True]],