Skip to content

relationalai.std.dates.isoweekday()

isoweekday(date: date|datetime|Producer, tz: str|Producer = "UTC") -> Expression

Computes the ISO weekday (1–7) from a date or UTC datetime value, where Monday is 1 and Sunday is 7. If tz is specified, then date is converted from UTC to the specified timezone before computing the ISO weekday. tz is ignored if date is a Python date or datetime object. If any of the arguments are Producer objects, then isoweekday() also acts as a filter and removes invalid values from the producer. Must be called in a rule or query context.

NameTypeDescription
dateProducer or Python date or datetime objectThe date or datetime value from which to compute the ISO weekday.
tzProducer or Python str objectOptional timezone string (e.g., "America/New_York"), offset string (e.g., "+0600"), or Python tzinfo object. Refer to the timezone database for a list of valid timezone identifiers. Ignored if date is a Python date or datetime object. (Default: "UTC").

An Expression object.

TypeError if the date parameter is not a Producer object or Python date or datetime object.

Use isoweekday() to compute the ISO weekday from date and datetime values:

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(time=dates.datetime(2021, 1, 1, 1, 30))
Event.add(id=2).set(time="invalid")
# =======
# EXAMPLE
# =======
with model.rule():
event = Event()
# isoweekday() filters out any events with invalid time values, so the
# following only sets the isoweekday property for Event 1.
event.set(isoweekday=dates.isoweekday(event.time))
# Since Event 2 is filtered out above, the following only sets the
# has_valid_time property for Event 1.
event.set(has_valid_time=True)
with model.query() as select:
event = Event()
response = select(event.id, event.time, event.isoweekday, event.has_valid_time)
print(response.results)
# id time isoweekday has_valid_time
# 0 1 2021-01-01 01:30:00 5.0 True
# 1 2 invalid NaN NaN

If the date parameter is a datetime and the tz parameter is specified, the datetime is converted from UTC to the specified timezone before computing the ISO weekday:

with model.query() as select:
event = Event()
isoweekday1 = dates.isoweekday(event.time, tz="America/New_York")
isoweekday2 = dates.isoweekday(event.time, tz="+0600")
response = select(event.id, event.time, alias(isoweekday1, "isoweekday1"), alias(isoweekday2, "isoweekday2"))
print(response.results)
# id time weekday1 weekday2
# 0 1 2021-01-01 01:30:00 3 4