Skip to content

relationalai.std.dates.days()

days(n: int|Producer) -> Expression

Creates a period of n days. Negative values are supported. If n is a Producer object, then days() also acts as a filter and removes non-integer values from the producer. Must be called in a rule or query context.

NameTypeDescription
nProducer or Python intThe number of days.

An Expression object.

Use days() to create a period of n days. You can add and subtract time periods from date or datetime values using the + and - operators:

import relationalai as rai
from relationalai.std import alias, dates
# =====
# SETUP
# =====
model = rai.Model("MyModel")
Event = model.Type("Event")
with model.rule():
Event.add(id=1).set(start=dates.date(2021, 1, 1), duration_days=1)
Event.add(id=2).set(start=dates.datetime(2021, 2, 1, 9, 30), duration_days=2)
Event.add(id=3).set(start=dates.date(2021, 3, 1), duration_days=-1)
Event.add(id=4).set(start=dates.date(2021, 4, 1), duration_days="invalid")
# =======
# EXAMPLE
# =======
with model.rule():
event = Event()
# days() filters out any events with invalid duration_days, so the following
# only sets the end property for Events 1, 2, and 3.
event.set(end=event.start + dates.days(event.duration_days))
# Since Event 4 is filtered out above, the following only sets the
# has_valid_duration property for Events 1, 2, and 3.
event.set(has_valid_duration=True)
with model.query() as select:
event = Event()
response = select(event.id, event.end, event.has_valid_duration)
print(response.results)
# id end has_valid_duration
# 0 1 2021-01-02 00:00:00 True
# 1 2 2021-02-03 09:30:00 True
# 2 3 2021-02-28 00:00:00 True
# 3 4 NaT NaN

Note that days() does not produce an integer value. In particular, you cannot do arithmetic with days() and numeric values:

with model.query() as select:
result = dates.days(2) / 365
response = select(result)
# Returns an empty result set because `days(2) / 365` is not a valid expression.
print(response.results)
# Empty DataFrame
# Columns: []
# Index: []