Skip to content

Commit

Permalink
feat(polars): add support for timestamp bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrist authored and gforsyth committed Oct 20, 2023
1 parent 1ffac11 commit c59518c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
28 changes: 28 additions & 0 deletions ibis/backends/polars/compiler.py
Expand Up @@ -751,6 +751,34 @@ def temporal_truncate(op, **kw):
return arg.dt.truncate(unit, "-1w")


def _compile_literal_interval(op):
if not isinstance(op, ops.Literal):
raise com.UnsupportedOperationError(
"Only literal interval values are supported"
)

if op.dtype.unit.short == "M":
suffix = "mo"
else:
suffix = op.dtype.unit.short.lower()

return f"{op.value}{suffix}"


@translate.register(ops.TimestampBucket)
def timestamp_bucket(op, **kw):
arg = translate(op.arg, **kw)
interval = _compile_literal_interval(op.interval)
if op.offset is not None:
offset = _compile_literal_interval(op.offset)
neg_offset = offset[1:] if offset.startswith("-") else f"-{offset}"
arg = arg.dt.offset_by(neg_offset)
else:
offset = None
res = arg.dt.truncate(interval, offset)
return res


@translate.register(ops.DateFromYMD)
def date_from_ymd(op, **kw):
return pl.date(
Expand Down
2 changes: 0 additions & 2 deletions ibis/backends/tests/test_temporal.py
Expand Up @@ -2525,7 +2525,6 @@ def test_delta(con, start, end, unit, expected):
"mysql",
"oracle",
"pandas",
"polars",
"pyspark",
"snowflake",
"sqlite",
Expand Down Expand Up @@ -2573,7 +2572,6 @@ def test_timestamp_bucket(backend, kws, pd_freq):
"mysql",
"oracle",
"pandas",
"polars",
"pyspark",
"snowflake",
"sqlite",
Expand Down

0 comments on commit c59518c

Please sign in to comment.