Skip to content

Commit 94d575a

Browse files
BUG: Raise if an aggregation function other than mean is used with ewm and times (#57225)
* BUG: Raise if an aggregation function other than mean is used with ewm (#51695) * python 3.9 and mypy issue
1 parent e69a051 commit 94d575a

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

doc/source/whatsnew/v3.0.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ Plotting
206206
Groupby/resample/rolling
207207
^^^^^^^^^^^^^^^^^^^^^^^^
208208
- Bug in :meth:`.DataFrameGroupBy.quantile` when ``interpolation="nearest"`` is inconsistent with :meth:`DataFrame.quantile` (:issue:`47942`)
209-
-
209+
- Bug in :meth:`DataFrame.ewm` and :meth:`Series.ewm` when passed ``times`` and aggregation functions other than mean (:issue:`51695`)
210210

211211
Reshaping
212212
^^^^^^^^^

pandas/core/window/ewm.py

+12
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,8 @@ def sum(
587587
):
588588
if not self.adjust:
589589
raise NotImplementedError("sum is not implemented with adjust=False")
590+
if self.times is not None:
591+
raise NotImplementedError("sum is not implemented with times")
590592
if maybe_use_numba(engine):
591593
if self.method == "single":
592594
func = generate_numba_ewm_func
@@ -658,6 +660,8 @@ def std(self, bias: bool = False, numeric_only: bool = False):
658660
raise NotImplementedError(
659661
f"{type(self).__name__}.std does not implement numeric_only"
660662
)
663+
if self.times is not None:
664+
raise NotImplementedError("std is not implemented with times")
661665
return zsqrt(self.var(bias=bias, numeric_only=numeric_only))
662666

663667
@doc(
@@ -691,6 +695,8 @@ def std(self, bias: bool = False, numeric_only: bool = False):
691695
agg_method="var",
692696
)
693697
def var(self, bias: bool = False, numeric_only: bool = False):
698+
if self.times is not None:
699+
raise NotImplementedError("var is not implemented with times")
694700
window_func = window_aggregations.ewmcov
695701
wfunc = partial(
696702
window_func,
@@ -753,6 +759,9 @@ def cov(
753759
bias: bool = False,
754760
numeric_only: bool = False,
755761
):
762+
if self.times is not None:
763+
raise NotImplementedError("cov is not implemented with times")
764+
756765
from pandas import Series
757766

758767
self._validate_numeric_only("cov", numeric_only)
@@ -837,6 +846,9 @@ def corr(
837846
pairwise: bool | None = None,
838847
numeric_only: bool = False,
839848
):
849+
if self.times is not None:
850+
raise NotImplementedError("corr is not implemented with times")
851+
840852
from pandas import Series
841853

842854
self._validate_numeric_only("corr", numeric_only)

pandas/tests/window/test_ewm.py

+12
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,18 @@ def test_ewm_sum_adjust_false_notimplemented():
173173
data.sum()
174174

175175

176+
@pytest.mark.parametrize("method", ["sum", "std", "var", "cov", "corr"])
177+
def test_times_only_mean_implemented(frame_or_series, method):
178+
# GH 51695
179+
halflife = "1 day"
180+
times = date_range("2000", freq="D", periods=10)
181+
ewm = frame_or_series(range(10)).ewm(halflife=halflife, times=times)
182+
with pytest.raises(
183+
NotImplementedError, match=f"{method} is not implemented with times"
184+
):
185+
getattr(ewm, method)()
186+
187+
176188
@pytest.mark.parametrize(
177189
"expected_data, ignore",
178190
[[[10.0, 5.0, 2.5, 11.25], False], [[10.0, 5.0, 5.0, 12.5], True]],

0 commit comments

Comments
 (0)