Skip to content

Commit 975794d

Browse files
committed
Fix ewm times, decay validation
1 parent 6f33c46 commit 975794d

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

doc/source/whatsnew/v1.5.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,7 @@ Groupby/resample/rolling
736736
- Bug in :meth:`.Rolling.var` would segfault calculating weighted variance when window size was larger than data size (:issue:`46760`)
737737
- Bug in :meth:`Grouper.__repr__` where ``dropna`` was not included. Now it is (:issue:`46754`)
738738
- Bug in :meth:`DataFrame.rolling` gives ValueError when center=True, axis=1 and win_type is specified (:issue:`46135`)
739+
- Bug in :class:`.ExponentialMovingWindow` where ``alpha``, ``com``, or ``span`` were incorrectly allowed when ``times`` and ``halflife`` were passed (:issue:`47003`)
739740

740741
Reshaping
741742
^^^^^^^^^

pandas/core/window/ewm.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -396,12 +396,10 @@ def __init__(
396396
if isna(self.times).any():
397397
raise ValueError("Cannot convert NaT values to integer")
398398
self._deltas = _calculate_deltas(self.times, self.halflife)
399-
# Halflife is no longer applicable when calculating COM
400-
# But allow COM to still be calculated if the user passes other decay args
401-
if common.count_not_none(self.com, self.span, self.alpha) > 0:
402-
self._com = get_center_of_mass(self.com, self.span, None, self.alpha)
403-
else:
404-
self._com = 1.0
399+
# GH 47003
400+
# get_center_of_mass will validate and raise if the user has also
401+
# passed in com, span or alpha (1.0 is a placeholder value)
402+
self._com = get_center_of_mass(self.com, self.span, 1.0, self.alpha)
405403
else:
406404
if self.halflife is not None and isinstance(
407405
self.halflife, (str, datetime.timedelta)

pandas/tests/window/test_ewm.py

+8
Original file line numberDiff line numberDiff line change
@@ -666,3 +666,11 @@ def test_ewm_pairwise_cov_corr(func, frame):
666666
result.index = result.index.droplevel(1)
667667
expected = getattr(frame[1].ewm(span=10, min_periods=5), func)(frame[5])
668668
tm.assert_series_equal(result, expected, check_names=False)
669+
670+
671+
@pytest.mark.parametrize("decay", ["alpha", "com", "span"])
672+
def test_validate_times_halflife_with_other_decay(decay):
673+
ser = Series([1, 2])
674+
msg = "comass, span, halflife, and alpha are mutually exclusive"
675+
with pytest.raises(ValueError, match=msg):
676+
ser.ewm(**{decay: 1}, halflife="1 Day", times=DatetimeIndex(["2021", "2022"]))

0 commit comments

Comments
 (0)