Skip to content

Commit 6aaa183

Browse files
authored
DOC: Clarify decay argument validation in ewm when times is provided (#47026)
1 parent 7c054d6 commit 6aaa183

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

doc/source/whatsnew/v1.5.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ Other enhancements
150150
- Added ``validate`` argument to :meth:`DataFrame.join` (:issue:`46622`)
151151
- A :class:`errors.PerformanceWarning` is now thrown when using ``string[pyarrow]`` dtype with methods that don't dispatch to ``pyarrow.compute`` methods (:issue:`42613`)
152152
- Added ``numeric_only`` argument to :meth:`Resampler.sum`, :meth:`Resampler.prod`, :meth:`Resampler.min`, :meth:`Resampler.max`, :meth:`Resampler.first`, and :meth:`Resampler.last` (:issue:`46442`)
153+
- ``times`` argument in :class:`.ExponentialMovingWindow` now accepts ``np.timedelta64`` (:issue:`47003`)
153154

154155
.. ---------------------------------------------------------------------------
155156
.. _whatsnew_150.notable_bug_fixes:

pandas/core/window/ewm.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,9 @@ class ExponentialMovingWindow(BaseWindow):
134134
r"""
135135
Provide exponentially weighted (EW) calculations.
136136
137-
Exactly one parameter: ``com``, ``span``, ``halflife``, or ``alpha`` must be
138-
provided.
137+
Exactly one of ``com``, ``span``, ``halflife``, or ``alpha`` must be
138+
provided if ``times`` is not provided. If ``times`` is provided,
139+
``halflife`` and one of ``com``, ``span`` or ``alpha`` may be provided.
139140
140141
Parameters
141142
----------
@@ -155,7 +156,7 @@ class ExponentialMovingWindow(BaseWindow):
155156
:math:`\alpha = 1 - \exp\left(-\ln(2) / halflife\right)`, for
156157
:math:`halflife > 0`.
157158
158-
If ``times`` is specified, the time unit (str or timedelta) over which an
159+
If ``times`` is specified, a timedelta convertible unit over which an
159160
observation decays to half its value. Only applicable to ``mean()``,
160161
and halflife value will not apply to the other functions.
161162
@@ -389,10 +390,8 @@ def __init__(
389390
raise ValueError("times must be datetime64[ns] dtype.")
390391
if len(self.times) != len(obj):
391392
raise ValueError("times must be the same length as the object.")
392-
if not isinstance(self.halflife, (str, datetime.timedelta)):
393-
raise ValueError(
394-
"halflife must be a string or datetime.timedelta object"
395-
)
393+
if not isinstance(self.halflife, (str, datetime.timedelta, np.timedelta64)):
394+
raise ValueError("halflife must be a timedelta convertible object")
396395
if isna(self.times).any():
397396
raise ValueError("Cannot convert NaT values to integer")
398397
self._deltas = _calculate_deltas(self.times, self.halflife)
@@ -404,7 +403,7 @@ def __init__(
404403
self._com = 1.0
405404
else:
406405
if self.halflife is not None and isinstance(
407-
self.halflife, (str, datetime.timedelta)
406+
self.halflife, (str, datetime.timedelta, np.timedelta64)
408407
):
409408
raise ValueError(
410409
"halflife can only be a timedelta convertible argument if "

pandas/tests/window/conftest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def engine_and_raw(request):
102102
return request.param
103103

104104

105-
@pytest.fixture(params=["1 day", timedelta(days=1)])
105+
@pytest.fixture(params=["1 day", timedelta(days=1), np.timedelta64(1, "D")])
106106
def halflife_with_times(request):
107107
"""Halflife argument for EWM when times is specified."""
108108
return request.param

pandas/tests/window/test_ewm.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def test_ewma_times_not_same_length():
9090

9191

9292
def test_ewma_halflife_not_correct_type():
93-
msg = "halflife must be a string or datetime.timedelta object"
93+
msg = "halflife must be a timedelta convertible object"
9494
with pytest.raises(ValueError, match=msg):
9595
Series(range(5)).ewm(halflife=1, times=np.arange(5).astype("datetime64[ns]"))
9696

0 commit comments

Comments
 (0)