date_range()#

relationalai.std.dates
#date_range(start: date | datetime | None = None, end: date | datetime | None = None, periods: int = 1, freq: str = "D") -> Expression

Creates a range of Date or DateTime values. If both start and end arguments are provided, they must both be dates or both be datetimes. Must be used within a rule or query context.

Parameters#

NameTypeDescription
startdate or datetime or NoneOptional start of the range (default is None). If start and end are both provided, they must be of the same type.
enddate or datetime or NoneOptional end of the range (default is None). If start and end are both provided, they must be of the same type.
periodsintOptional number of periods in the range (default is 1).
freqstrThe frequency of the periods (default is "D" for days). Accepted values are:
  • "us": microseconds
  • "ms": milliseconds
  • "s": seconds
  • "m": minutes
  • "H": hours
  • "D": days
  • "W": weeks
  • "M": months
  • "Y": years

Returns#

An Expression that produces a range of Date or DateTime values.

Example#

Use the date_range() function to create various ranges of dates and datetimes.

Get a range of dates from January 1, 2024 to January 7, 2024:

#from relationalai.std.dates import date, date_range

# Create a model named "events" with an `Event` type.
model = rai.Model("events")
Event = model.Type("Event")

# Get a range of dates.
with model.query() as select:
    # NOTE: 'start' and 'end' must be of the same type.
    dates = date_range(date(2024, 1, 1), date(2024, 1, 7))
    response = select(dates)

print(response.results)
#            v
# 0 2024-01-01
# 1 2024-01-02
# 2 2024-01-03
# 3 2024-01-04
# 4 2024-01-05
# 5 2024-01-06
# 6 2024-01-07

You can use DateTime values instead of Date values:

#from relationalai.std.dates import datetime, date_range

# Get a range of datetimes with hourly frequency.
with model.query() as select:
    times = date_range(
        start=datetime(2024, 1, 1, 0, 0, 0),
        end=datetime(2024, 1, 1, 12, 0, 0),
        freq="H"
    )
    response = select(times)

print(response.results)
#                      v
# 0  2024-01-01 00:00:00
# 1  2024-01-01 01:00:00
# 2  2024-01-01 02:00:00
# 3  2024-01-01 03:00:00
# 4  2024-01-01 04:00:00
# 5  2024-01-01 05:00:00
# 6  2024-01-01 06:00:00
# 7  2024-01-01 07:00:00
# 8  2024-01-01 08:00:00
# 9  2024-01-01 09:00:00
# 10 2024-01-01 10:00:00
# 11 2024-01-01 11:00:00
# 12 2024-01-01 12:00:00

Using a sub-day frequency with a Date value produces a range of DateTime values:

#from relationalai.std.dates import date, date_range

# Get a range of datetimes with hourly frequency.
with model.query() as select:
    times = date_range(date(2024, 1, 1), date(2024, 1, 2), freq='H')
    response = select(times)

print(response.results)
#                      v
# 0  2024-01-01 00:00:00
# 1  2024-01-01 01:00:00
# 2  2024-01-01 02:00:00
# 3  2024-01-01 03:00:00
# 4  2024-01-01 04:00:00
# 5  2024-01-01 05:00:00
# 6  2024-01-01 06:00:00
# 7  2024-01-01 07:00:00
# 8  2024-01-01 08:00:00
# 9  2024-01-01 09:00:00
# 10 2024-01-01 10:00:00
# 11 2024-01-01 11:00:00
# 12 2024-01-01 12:00:00
# 13 2024-01-01 13:00:00
# 14 2024-01-01 14:00:00
# 15 2024-01-01 15:00:00
# 16 2024-01-01 16:00:00
# 17 2024-01-01 17:00:00
# 18 2024-01-01 18:00:00
# 19 2024-01-01 19:00:00
# 20 2024-01-01 20:00:00
# 21 2024-01-01 21:00:00
# 22 2024-01-01 22:00:00
# 23 2024-01-01 23:00:00
# 24 2024-01-02 00:00:00

Create a range without an end date by specifying only the number of periods:

#from relationalai.std.dates import datetime, date_range

# Create a range without an end date.
with model.query() as select:
    times = date_range(datetime(2024, 1, 1, 0, 0, 0, 0), periods=3, freq='D')
    response = select(times)

print(response.results)
#            v
# 0 2024-01-01
# 1 2024-01-02
# 2 2024-01-03

Create a range that ends at a specified end date:

#from relationalai.std.dates import date, date_range

# Create a range that ends at the specified end date.
with model.query() as select:
    dates = date_range(end=date(2024, 1, 7), periods=7, freq='D')
    response = select(dates)

print(response.results)
#            v
# 0 2024-01-01
# 1 2024-01-02
# 2 2024-01-03
# 3 2024-01-04
# 4 2024-01-05
# 5 2024-01-06
# 6 2024-01-07

See Also#