pandas
pandas copied to clipboard
Allow adjust=False when times is provided
- [ x ] closes #54328 (partially)
- [ x ] Tests added and passed if fixing a bug or adding a new feature
- [ x ] All code checks passed.
- [ x ] Added type annotations to new arguments/methods/functions.
- [ x ] Added an entry in the latest
doc/source/whatsnew/v3.0.0.rst
This change enables EWMA to be calculated recursively ("infinite history", i.e. adjust=False
) for irregular-interval time series. Previously new_wt
was held constant in the ewm run loop, but now new_wt
is updated each step through the series under the narrow condition that adjust=False
and com=1
.
There may be a more elegant expression that will generalize to com
values different than one, but using 1 - old_wt
works nicely if halflife
is the only provided decay parameter since we are guaranteed com=1
. The additional parameter restrictions to force this condition seem acceptable since there is already some restriction on decay parameters.
Exactly one of
com
,span
,halflife
, oralpha
must be provided iftimes
is not provided. Iftimes
is provided,halflife
and one ofcom
,span
oralpha
may be provided.
FWIW I confirmed that results match polars and @azmyrajab's nice test function:
ema_no_adjust = ema_test(vals, dt_seconds, half_life=half_life_seconds)[-1]
ema_no_adjust_pl = ema_polars(df["val"].rename_axis(index="ts").rename("val"), half_life,
by="ts").select("val")[-1].item()
print(ema_no_adjust)
print(ema_no_adjust_pl)
print(df.ewm(halflife=half_life, times=id, adjust=False).mean().iloc[-1]['val'])
0.2062994740159002
0.2062994740159002
0.2062994740159002