Date()#

relationalai.std.dates
#Date(val: Producer) -> Expression

Filters values produced by a Producer object to only include date values. Must be called in a rule or query context.

Parameters#

NameTypeDescription
valProducerThe values to filter.

Returns#

An Expression object.

Example#

Use Date() to filter date values in a rule or query:

#import relationalai as rai
from relationalai.std import alias, dates


# =====
# SETUP
# =====

model = rai.Model("MyModel")
Person = model.Type("Person")

with model.rule():
    Person.add(name="Alice", birth_date="2021-01-01")  # birth_date is a string.
    Person.add(name="Bob", birth_date=dates.date(2021, 1, 1)) # birth_date is a date.


# =======
# EXAMPLE
# =======

# Get the names and birth dates of people where 'birth_date' is a date.
with model.query() as select:
    person = Person()
    dates.Date(person.birth_date)  # Filter date values.
    response = select(person.name, person.birth_date)

print(response.results)
#   name birth_date
# 0  Bob 2021-01-01

You can use Date() in a Model.match() block to conditionally write a function that returns an Expression that produces True or False depending on whether the input is a date value.

#def is_date(val):
    with model.match() as result:
        with model.case():
            dates.Date(val)
            result.add(True)
        with model.case():
            result.add(False)
    return result

with model.query() as select:
    person = Person()
    response = select(
        person.name,
        person.birth_date,
        alias(is_date(person.birth_date), "is_date"),
    )

print(response.results)
#     name           birth_date  is_date
# 0  Alice           2021-01-01    False
# 1    Bob  2021-01-01 00:00:00     True

See Also#