Skip to content

DOC: Clarify decay argument validation in ewm when times is provided #47026

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 5 commits into from
May 18, 2022
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ Other enhancements
- Added ``validate`` argument to :meth:`DataFrame.join` (:issue:`46622`)
- A :class:`errors.PerformanceWarning` is now thrown when using ``string[pyarrow]`` dtype with methods that don't dispatch to ``pyarrow.compute`` methods (:issue:`42613`)
- 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`)
- ``times`` argument in :class:`.ExponentialMovingWindow` now accepts ``np.timedelta64`` (:issue:`47003`)

.. ---------------------------------------------------------------------------
.. _whatsnew_150.notable_bug_fixes:
Expand Down
15 changes: 7 additions & 8 deletions pandas/core/window/ewm.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,9 @@ class ExponentialMovingWindow(BaseWindow):
r"""
Provide exponentially weighted (EW) calculations.
Exactly one parameter: ``com``, ``span``, ``halflife``, or ``alpha`` must be
provided.
Exactly one of ``com``, ``span``, ``halflife``, or ``alpha`` must be
provided if ``times`` is not provided. If ``times`` is provided,
``halflife`` and one of ``com``, ``span`` or ``alpha`` may be provided.
Parameters
----------
Expand All @@ -155,7 +156,7 @@ class ExponentialMovingWindow(BaseWindow):
:math:`\alpha = 1 - \exp\left(-\ln(2) / halflife\right)`, for
:math:`halflife > 0`.
If ``times`` is specified, the time unit (str or timedelta) over which an
If ``times`` is specified, a timedelta convertible unit over which an
observation decays to half its value. Only applicable to ``mean()``,
and halflife value will not apply to the other functions.
Expand Down Expand Up @@ -389,10 +390,8 @@ def __init__(
raise ValueError("times must be datetime64[ns] dtype.")
if len(self.times) != len(obj):
raise ValueError("times must be the same length as the object.")
if not isinstance(self.halflife, (str, datetime.timedelta)):
raise ValueError(
"halflife must be a string or datetime.timedelta object"
)
if not isinstance(self.halflife, (str, datetime.timedelta, np.timedelta64)):
raise ValueError("halflife must be a timedelta convertible object")
if isna(self.times).any():
raise ValueError("Cannot convert NaT values to integer")
self._deltas = _calculate_deltas(self.times, self.halflife)
Expand All @@ -404,7 +403,7 @@ def __init__(
self._com = 1.0
else:
if self.halflife is not None and isinstance(
self.halflife, (str, datetime.timedelta)
self.halflife, (str, datetime.timedelta, np.timedelta64)
):
raise ValueError(
"halflife can only be a timedelta convertible argument if "
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/window/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def engine_and_raw(request):
return request.param


@pytest.fixture(params=["1 day", timedelta(days=1)])
@pytest.fixture(params=["1 day", timedelta(days=1), np.timedelta64(1, "D")])
def halflife_with_times(request):
"""Halflife argument for EWM when times is specified."""
return request.param
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/window/test_ewm.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def test_ewma_times_not_same_length():


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

Expand Down