Skip to content

Is there a way to create lags based on the sampling interval? #9035

Answered by cpcloud
kaijennissen asked this question in Q&A
Discussion options

You must be logged in to vote

You should be able do this with a bit of window function-fu:

In [35]: from ibis.interactive import *

In [36]: import datetime as dt

In [37]: 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",
    ...: )

In [38]: t.mutate(value_lag_1=(_.date.delta(_.date.lag().over(group_by="store_id"), 'days') == 1).cast('int').nullif(0) * _.value.lag().over(group_by="store_id")).order_by("store_id", "date")
Out[38]:
┏━━━━━━━━━━┳…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by cpcloud
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants