From fe5da33d18aaf3f66f058915d18d789ae15268dd Mon Sep 17 00:00:00 2001 From: Steven Schaerer <53116297+stevenschaerer@users.noreply.github.com> Date: Fri, 2 Feb 2024 23:05:08 +0100 Subject: [PATCH 1/2] 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 | 14 ++++++++++++++ 3 files changed, 27 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..7147c7e5be039 100644 --- a/pandas/tests/window/test_ewm.py +++ b/pandas/tests/window/test_ewm.py @@ -173,6 +173,20 @@ def test_ewm_sum_adjust_false_notimplemented(): data.sum() +@pytest.mark.parametrize("method", ["sum", "std", "var", "cov", "corr"]) +def test_times_only_mean_implemented( + frame_or_series: (type[DataFrame] | type[Series]), method: str +): + # 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]], From ffdda3894b4517cb1ab48db39fc556bb4a0237aa Mon Sep 17 00:00:00 2001 From: Steven Schaerer <53116297+stevenschaerer@users.noreply.github.com> Date: Sat, 3 Feb 2024 17:35:46 +0100 Subject: [PATCH 2/2] python 3.9 and mypy issue --- pandas/tests/window/test_ewm.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pandas/tests/window/test_ewm.py b/pandas/tests/window/test_ewm.py index 7147c7e5be039..35c896dc0090b 100644 --- a/pandas/tests/window/test_ewm.py +++ b/pandas/tests/window/test_ewm.py @@ -174,9 +174,7 @@ def test_ewm_sum_adjust_false_notimplemented(): @pytest.mark.parametrize("method", ["sum", "std", "var", "cov", "corr"]) -def test_times_only_mean_implemented( - frame_or_series: (type[DataFrame] | type[Series]), method: str -): +def test_times_only_mean_implemented(frame_or_series, method): # GH 51695 halflife = "1 day" times = date_range("2000", freq="D", periods=10)