date.fromisoformat()#
relationalai.std.dates
#date.fromisoformat(date_string: str | Producer) -> Expression
Constructs a date value from an ISO 8601 date string.
Only strings in the format YYYY-MM-DD
are supported.
Strings that do not match the format are filtered out.
Must be called in a rule or query context.
Parameters#
Name | Type | Description |
---|---|---|
date_string | str or Producer | The ISO 8601 date string to convert to a date value. |
Returns#
An Expression
object.
Example#
Use date.fromisoformat()
to construct a date value from an ISO 8601 date string in the YYYY-MM-DD
format:
#import relationalai as rai
from relationalai.std.dates import date
# =====
# SETUP
# =====
model = rai.Model("MyModel")
Person = model.Type("Person")
with model.rule():
Person.add(id=1).set(name="Alice", birthdate_str="2001-01-01") # Valid ISO 8601 date format.
Person.add(id=2).set(name="Bob", birthdate_str="02/02/2002") # Invalid format.
# =======
# EXAMPLE
# =======
with model.rule():
person = Person()
# Set the birthdate property to a date value parsed from the birthdate_str
# property. If person.birthdate_str is not a valid ISO 8601 date format, it
# is filtered out and the birthdate property is not set.
person.set(birthdate=date.fromisoformat(person.birthdate_str))
with model.query() as select:
person = Person()
response = select(person.name, person.birthdate_str, person.birthdate)
print(response.results)
# name birthdate_str birthdate
# 0 Alice 2001-01-01 2001-01-01
# 1 Bob 02/02/2002 NaT