Is there a way to create lags based on the sampling interval? #9035
Answered
by
cpcloud
kaijennissen
asked this question in
Q&A
-
When working with time-series it's common to use lagged features, where the lags are wrt. the sampling interval. If the sampling interval is f.e. daily, then a lag of 1 returns the value of the previous day. I was able to come up with a solution which works by first adding the sampling-interval and then joining the newly created table. Is there a way to get the same result without intermediate table? import datetime as dt
import ibis
from ibis import _
ibis.options.interactive = True
t = ibis.memtable(
{
"store_id": [1] * 5 + [2] * 5,
"date": [dt.datetime(2022, 1, x) for x in [2, 3, 4, 5, 6]]
+ [dt.datetime(2022, 1, x) for x in [2, 5, 6, 7, 9]],
"value": [1, 2, 3, 4, 5] + [1, 4, 5, 6, 8],
},
name="t",
)
t_lag = t.mutate(lag_date=_.date + ibis.interval(days=1)).rename(value_lag_1="value")
t.join(
t_lag,
[t.store_id == t_lag.store_id, t.date == t_lag.lag_date],
how="left",
).drop(_.lag_date, _.store_id_right, _.date_right).order_by(t.store_id, t.date)
|
Beta Was this translation helpful? Give feedback.
Answered by
cpcloud
Aug 1, 2024
Replies: 1 comment
-
You should be able do this with a bit of window function-fu:
|
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
cpcloud
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You should be able do this with a bit of window function-fu: