Skip to content

DateFilter

After

Bases: Date

Represents a threshold where a date must be strictly after the specified value.

Source code in phenex/filters/date_filter.py
class After(Date):
    """
    Represents a threshold where a date must be strictly after the specified value.
    """

    def __init__(self, value: Union[date, str], **kwargs):
        super(After, self).__init__(">", value)

__str__()

Generate a human-readable string like 'greater than 18'.

Source code in phenex/filters/value.py
def __str__(self) -> str:
    """Generate a human-readable string like 'greater than 18'."""
    operator_text_map = {
        ">": "greater than",
        ">=": "greater than or equal to",
        "<": "less than",
        "<=": "less than or equal to",
        "=": "equal to",
    }
    op_text = operator_text_map.get(self.operator, self.operator)
    return f"{op_text} {self.value}"

to_short_string()

Generate a short string representation like 'g18' or 'le65'.

Source code in phenex/filters/value.py
def to_short_string(self) -> str:
    """Generate a short string representation like 'g18' or 'le65'."""
    operator_map = {">": "g", ">=": "ge", "<": "l", "<=": "le", "=": "eq"}
    op = operator_map.get(self.operator, self.operator)

    # Format as int if it's a whole number, otherwise keep decimals
    value = self.value
    if isinstance(value, (int, float)) and value == int(value):
        return f"{op}{int(value)}"
    else:
        return f"{op}{value}"

AfterOrOn

Bases: Date

Represents a threshold where a date must be on or after the specified value.

Source code in phenex/filters/date_filter.py
class AfterOrOn(Date):
    """
    Represents a threshold where a date must be on or after the specified value.
    """

    def __init__(self, value: Union[date, str], **kwargs):
        super(AfterOrOn, self).__init__(">=", value)

__str__()

Generate a human-readable string like 'greater than 18'.

Source code in phenex/filters/value.py
def __str__(self) -> str:
    """Generate a human-readable string like 'greater than 18'."""
    operator_text_map = {
        ">": "greater than",
        ">=": "greater than or equal to",
        "<": "less than",
        "<=": "less than or equal to",
        "=": "equal to",
    }
    op_text = operator_text_map.get(self.operator, self.operator)
    return f"{op_text} {self.value}"

to_short_string()

Generate a short string representation like 'g18' or 'le65'.

Source code in phenex/filters/value.py
def to_short_string(self) -> str:
    """Generate a short string representation like 'g18' or 'le65'."""
    operator_map = {">": "g", ">=": "ge", "<": "l", "<=": "le", "=": "eq"}
    op = operator_map.get(self.operator, self.operator)

    # Format as int if it's a whole number, otherwise keep decimals
    value = self.value
    if isinstance(value, (int, float)) and value == int(value):
        return f"{op}{int(value)}"
    else:
        return f"{op}{value}"

Before

Bases: Date

Represents a threshold where a date must be strictly before the specified value.

Source code in phenex/filters/date_filter.py
class Before(Date):
    """
    Represents a threshold where a date must be strictly before the specified value.
    """

    def __init__(self, value: Union[date, str], **kwargs):
        super(Before, self).__init__("<", value)

__str__()

Generate a human-readable string like 'greater than 18'.

Source code in phenex/filters/value.py
def __str__(self) -> str:
    """Generate a human-readable string like 'greater than 18'."""
    operator_text_map = {
        ">": "greater than",
        ">=": "greater than or equal to",
        "<": "less than",
        "<=": "less than or equal to",
        "=": "equal to",
    }
    op_text = operator_text_map.get(self.operator, self.operator)
    return f"{op_text} {self.value}"

to_short_string()

Generate a short string representation like 'g18' or 'le65'.

Source code in phenex/filters/value.py
def to_short_string(self) -> str:
    """Generate a short string representation like 'g18' or 'le65'."""
    operator_map = {">": "g", ">=": "ge", "<": "l", "<=": "le", "=": "eq"}
    op = operator_map.get(self.operator, self.operator)

    # Format as int if it's a whole number, otherwise keep decimals
    value = self.value
    if isinstance(value, (int, float)) and value == int(value):
        return f"{op}{int(value)}"
    else:
        return f"{op}{value}"

BeforeOrOn

Bases: Date

Represents a threshold where a date must be on or before the specified value.

Source code in phenex/filters/date_filter.py
class BeforeOrOn(Date):
    """
    Represents a threshold where a date must be on or before the specified value.
    """

    def __init__(self, value: Union[date, str], **kwargs):
        super(BeforeOrOn, self).__init__("<=", value)

__str__()

Generate a human-readable string like 'greater than 18'.

Source code in phenex/filters/value.py
def __str__(self) -> str:
    """Generate a human-readable string like 'greater than 18'."""
    operator_text_map = {
        ">": "greater than",
        ">=": "greater than or equal to",
        "<": "less than",
        "<=": "less than or equal to",
        "=": "equal to",
    }
    op_text = operator_text_map.get(self.operator, self.operator)
    return f"{op_text} {self.value}"

to_short_string()

Generate a short string representation like 'g18' or 'le65'.

Source code in phenex/filters/value.py
def to_short_string(self) -> str:
    """Generate a short string representation like 'g18' or 'le65'."""
    operator_map = {">": "g", ">=": "ge", "<": "l", "<=": "le", "=": "eq"}
    op = operator_map.get(self.operator, self.operator)

    # Format as int if it's a whole number, otherwise keep decimals
    value = self.value
    if isinstance(value, (int, float)) and value == int(value):
        return f"{op}{int(value)}"
    else:
        return f"{op}{value}"

Date

Bases: Value

The Date class is a specialized Value class for handling date comparisons.

Attributes:

Name Type Description
operator str

The comparison operator, one of '>', '>=', '<', '<=', '='.

value Union[date, str]

The date value, which can be a date object or a string in 'YYYY-MM-DD' format.

date_format str

The format to use for parsing date strings (default is '%Y-%m-%d').

Source code in phenex/filters/date_filter.py
class Date(Value):
    """
    The Date class is a specialized Value class for handling date comparisons.

    Attributes:
        operator (str): The comparison operator, one of '>', '>=', '<', '<=', '='.
        value (Union[date, str]): The date value, which can be a `date` object or a string in 'YYYY-MM-DD' format.
        date_format (str): The format to use for parsing date strings (default is '%Y-%m-%d').
    """

    def __init__(self, operator: str, value: Union[date, str], date_format="%Y-%m-%d"):
        if isinstance(value, str):
            value = datetime.strptime(value, date_format).date()
        super(Date, self).__init__(operator, value)

__str__()

Generate a human-readable string like 'greater than 18'.

Source code in phenex/filters/value.py
def __str__(self) -> str:
    """Generate a human-readable string like 'greater than 18'."""
    operator_text_map = {
        ">": "greater than",
        ">=": "greater than or equal to",
        "<": "less than",
        "<=": "less than or equal to",
        "=": "equal to",
    }
    op_text = operator_text_map.get(self.operator, self.operator)
    return f"{op_text} {self.value}"

to_short_string()

Generate a short string representation like 'g18' or 'le65'.

Source code in phenex/filters/value.py
def to_short_string(self) -> str:
    """Generate a short string representation like 'g18' or 'le65'."""
    operator_map = {">": "g", ">=": "ge", "<": "l", "<=": "le", "=": "eq"}
    op = operator_map.get(self.operator, self.operator)

    # Format as int if it's a whole number, otherwise keep decimals
    value = self.value
    if isinstance(value, (int, float)) and value == int(value):
        return f"{op}{int(value)}"
    else:
        return f"{op}{value}"

DateFilter(min_date=None, max_date=None, column_name='EVENT_DATE')

DateFilter is a specialized ValueFilter for handling date-based filtering.

Parameters:

Name Type Description Default
min_date Optional[Union[Date, After, AfterOrOn]]

The minimum date condition. Recommended to pass either After or AfterOrOn.

None
max_date Optional[Union[Date, Before, BeforeOrOn]]

The maximum date condition. Recommended to pass either Before or BeforeOrOn.

None
column_name str

The name of the column to apply the filter on. Defaults to "EVENT_DATE".

'EVENT_DATE'
Source code in phenex/filters/date_filter.py
def DateFilter(
    min_date: Optional[Union[Date, After, AfterOrOn]] = None,
    max_date: Optional[Union[Date, Before, BeforeOrOn]] = None,
    column_name: str = "EVENT_DATE",
):
    """
    DateFilter is a specialized ValueFilter for handling date-based filtering.

    Parameters:
        min_date: The minimum date condition. Recommended to pass either After or AfterOrOn.
        max_date: The maximum date condition. Recommended to pass either Before or BeforeOrOn.
        column_name: The name of the column to apply the filter on. Defaults to "EVENT_DATE".
    """
    # For some reason, implementing DateFilter as a subclass of ValueFilter messes up the serialization. So instead we implement DateFilter as a function that looks like a class and just returns a ValueFilter instance.
    return ValueFilter(min_value=min_date, max_value=max_date, column_name=column_name)