Description
Feature Type
-
Adding new functionality to pandas
-
Changing existing functionality in pandas
-
Removing existing functionality in pandas
Problem Description
rolling()
allows passing a timedelta window when the DataFrame has a DatetimeIndex
. ewm()
requires passing the times
parameter when using a timedelta halflife with a DatetimeIndex
. I think ewm()
should default to using the index as times
if it is a DatetimeIndex
and halflife
is a timedelta. This way it would be more similar to the rolling()
behavior, and in my experience I've only ever seen df.index
passed as the times
argument. It seems counterintuitive to me to have to explicitly pass the index to ewm()
when working with times and timedeltas.
Feature Description
If the halflife
parameter to ewm()
is a timedelta and times
is not explicitly passed, use the index if it is a DatetimeIndex
.
Alternative Solutions
The alternative would be to keep the existing behavior.
Additional Context
Current:
>>> now = pd.Timestamp.now()
>>> idx = [now + pd.Timedelta(seconds=i) for i in range(1, 4)]
>>> df = pd.DataFrame({'a': [1,2,3]}, index=idx)
>>> df.ewm(halflife=pd.Timedelta(seconds=1)).mean()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/gsiano/.local/lib/python3.10/site-packages/pandas/core/generic.py", line 11743, in ewm
return ExponentialMovingWindow(
File "/home/gsiano/.local/lib/python3.10/site-packages/pandas/core/window/ewm.py", line 386, in __init__
raise ValueError(
ValueError: halflife can only be a timedelta convertible argument if times is not None.
>>> df.ewm(halflife=pd.Timedelta(seconds=1), times=df.index).mean()
a
2023-07-18 10:45:18.007264 1.000000
2023-07-18 10:45:19.007264 1.666667
2023-07-18 10:45:20.007264 2.428571
>>>
Proposed:
>>> now = pd.Timestamp.now()
>>> idx = [now + pd.Timedelta(seconds=i) for i in range(1, 4)]
>>> df = pd.DataFrame({'a': [1,2,3]}, index=idx)
>>> df.ewm(halflife=pd.Timedelta(seconds=1)).mean()
a
2023-07-18 10:46:46.589891 1.000000
2023-07-18 10:46:47.589891 1.666667
2023-07-18 10:46:48.589891 2.428571