Skip to content

Commit

Permalink
feat(api): accept more input types in ibis.range (#9659)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud authored Jul 22, 2024
1 parent 69c5bf0 commit 310ad30
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
4 changes: 3 additions & 1 deletion ibis/expr/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2237,7 +2237,9 @@ def _timestamp_range(
step: datetime.timedelta | ir.IntervalValue,
) -> ir.ArrayValue:
return ops.TimestampRange(
start=normalize_datetime(start), stop=normalize_datetime(stop), step=step
start=normalize_datetime(start) if isinstance(start, str) else start,
stop=normalize_datetime(stop) if isinstance(stop, str) else stop,
step=step,
).to_expr()


Expand Down
38 changes: 37 additions & 1 deletion ibis/tests/expr/test_timestamp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from datetime import datetime
from datetime import datetime, timedelta

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -184,3 +184,39 @@ def test_timestamp_field_access_on_time_failure(
def test_integer_timestamp_fails(value):
with pytest.raises(TypeError, match=r"Use ibis\.literal\(\.\.\.\)\.to_timestamp"):
ibis.timestamp(value)


@pytest.mark.parametrize(
"start",
[
"2002-01-01 00:00:00",
datetime(2002, 1, 1, 0, 0, 0),
ibis.timestamp("2002-01-01 00:00:00"),
ibis.timestamp(datetime(2002, 1, 1, 0, 0, 0)),
ibis.table({"start": "timestamp"}).start,
],
)
@pytest.mark.parametrize(
"stop",
[
"2002-01-02 00:00:00",
datetime(2002, 1, 2, 0, 0, 0),
ibis.timestamp("2002-01-02 00:00:00"),
ibis.timestamp(datetime(2002, 1, 2, 0, 0, 0)),
ibis.table({"stop": "timestamp"}).stop,
],
)
@pytest.mark.parametrize("step", [ibis.interval(seconds=1), timedelta(seconds=1)])
def test_timestamp_range_with_str_inputs(start, stop, step):
expr = ibis.range(start, stop, step)

op = expr.op()

assert op.start.dtype.is_timestamp()
assert op.stop.dtype.is_timestamp()
assert op.step.dtype.is_interval()

dtype = expr.type()

assert dtype.is_array()
assert dtype.value_type.is_timestamp()

0 comments on commit 310ad30

Please sign in to comment.