Skip to content

Commit

Permalink
BUG: Raise if an aggregation function other than mean is used with ewm (
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenschaerer committed Feb 2, 2024
1 parent 8ed7dae commit edc8b33
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v3.0.0.rst
Expand Up @@ -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
^^^^^^^^^
Expand Down
12 changes: 12 additions & 0 deletions pandas/core/window/ewm.py
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/window/test_ewm.py
Expand Up @@ -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]],
Expand Down

0 comments on commit edc8b33

Please sign in to comment.