Date()#
relationalai.std.dates
#Date(val: Any) -> Expression
Checks if a value is a date.
Must be called in a rule or query context.
Date values are constructed using the date()
function.
To check if a value is a datetime, use DateTime()
.
Parameters#
Name | Type | Description |
---|---|---|
val | Any | The value to check. |
Returns#
An Expression
that filters date values.
Example#
Use Date()
to check if a value is a date:
#import relationalai as rai
from relationalai.std.dates import Date, date
# Create a model named "people" with a `Person` type.
model = rai.Model("people")
Person = model.Type("Person")
# Add people to the model.
with model.rule():
# Add a person with a birth date as a string.
Person.add(name="Alice", birth_date="2021-01-01")
# Add a person with a birth date as a Date.
Person.add(name="Bob", birth_date=date(2021, 1, 1))
# Get names and birth dates of people, provided the
# .birth_date property is a Date value.
with model.query() as select:
person = Person()
Date(person.birth_date) # Filter Date values.
response = select(person.name, person.birth_date)
# Only people where 'birth_date' is a date are returned.
print(response.results)
# Output:
# name birth_date
# 0 Bob 2021-01-01
If val
produces no Date
values, Date(val)
will not filter any values,
possibly resulting in an empty query result:
#import relationalai as rai
from relationalai.std.dates import Date
# Create a model named "people" with a `Person` type.
model = rai.Model("people")
Person = model.Type("Person")
# Add a person to the model.
with model.rule():
Person.add(name="Bob", age=30)
with model.query() as select:
person = Person()
Date(person.age)
response = select(person.name)
print(response.results)
# Output:
# Empty DataFrame
# Columns: [name]
# Index: []
Date(person.birth_date)
does not produce True
or False
values.
Instead, it filters the values produced by person.birth_date
to only include Date
values.
If you need a True
or False
value, you can use a Model.match()
context:
#import relationalai as rai
from relationalai.std.dates import Date, date
# Create a model named "people" with a `Person` type.
model = rai.Model("people")
Person = model.Type("Person")
# Add some people to the model.
with model.rule():
Person.add(name="Alice", birth_date="2021-01-01") # birth_date is a string.
Person.add(name="Bob", birth_date=date(2021, 1, 1)) # birth_date is a date.
# Check if birth_date is a date.
with model.query() as select:
person = Person()
with model.match() as matched:
# If person.birth_date is a date, add True to matched.
with model.case():
Date(person.birth_date)
matched.add(person, is_date=True)
# Otherwise, add False.
with model.case():
matched.add(person, is_date=False)
response = select(person.name, person.birth_date, matched.is_date)
print(response.results)
# Output:
# name birth_date is_date
# 0 Alice 2021-01-01 False
# 1 Bob 2021-01-01 True