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. If date_string is a Producer, then date.fromisoformat() also acts as a filter and removes invalid strings and non-string values from the producer. Must be called in a rule or query context.

Parameters#

NameTypeDescription
date_stringstr or ProducerThe 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 import dates


# =====
# 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()
    person.set(birthdate=date.fromisoformat(person.birthdate_str))

    # date.fromisofomat() filters out any people with invalid date strings, so
    # the following only sets the has_valid_date property for Alice.
    person.set(has_valid_date=True)

with model.query() as select:
    person = Person()
    response = select(person.name, person.birthdate, person.has_valid_date)

print(response.results)
#     name  birthdate has_valid_date
# 0  Alice 2001-01-01           True
# 1    Bob        NaT            NaN

See Also#