Skip to content

Commit a10df1f

Browse files
fix r28 metrics for sparser data (#66)
1 parent ab035e1 commit a10df1f

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/ibis_analytics/metrics.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,30 @@ def total(t: ibis.Table) -> int:
1111
return t.count().to_pyarrow().as_py()
1212

1313

14+
def _densify(t: ibis.Table, ts_col: str, agg_col: str) -> ibis.Table:
15+
ts = (
16+
ibis.range(
17+
t[ts_col].min().as_scalar(),
18+
ibis.now().truncate("D"),
19+
step=ibis.interval(days=1),
20+
)
21+
.unnest()
22+
.name(ts_col)
23+
.as_table()
24+
)
25+
26+
return t.join(ts, ts_col, how="outer").select(
27+
**{ts_col: f"{ts_col}_right", agg_col: ibis._[agg_col].fill_null(0)},
28+
)
29+
30+
1431
def stars_rolling(t: ibis.Table, days: int = 28) -> ibis.Table:
1532
t = (
1633
t.mutate(starred_at=t["starred_at"].truncate("D"))
1734
.group_by("starred_at")
1835
.agg(stars=ibis._.count())
1936
)
37+
t = _densify(t, "starred_at", "stars")
2038
t = t.select(
2139
timestamp="starred_at",
2240
rolling_stars=ibis._["stars"]

0 commit comments

Comments
 (0)