Skip to content

BUG: Raise if an aggregation function other than mean is used with ewm and times #57225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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_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