Skip to content

DateRangeFilter

Bases: ValueFilter

DateRangeFilter is a ValueFilter applied to dates.

Attributes:

Name Type Description
min_date Optional[Union[date, str]]

The minimum date for the filter. If a string is provided, it will be converted to a date according to date_format.

max_date Optional[Union[date, str]]

The maximum date for the filter. If a string is provided, it will be converted to a date according to date_format.

column_name Optional[str]

The name of the column to apply the filter on. Defaults to EVENT_DATE, the default value for date columns in Phenex.

date_format str

The format to use for parsing date strings.

Source code in phenex/filters/date_range_filter.py
class DateRangeFilter(ValueFilter):
    """
    DateRangeFilter is a ValueFilter applied to dates.

    Attributes:
        min_date (Optional[Union[date, str]]): The minimum date for the filter. If a string is provided, it will be converted to a date according to date_format.
        max_date (Optional[Union[date, str]]): The maximum date for the filter. If a string is provided, it will be converted to a date according to date_format.
        column_name (Optional[str]): The name of the column to apply the filter on. Defaults to EVENT_DATE, the default value for date columns in Phenex.
        date_format (str): The format to use for parsing date strings.
    """

    def __init__(
        self,
        min_date: Optional[Union[date, str]] = None,
        max_date: Optional[Union[date, str]] = None,
        column_name: Optional[str] = "EVENT_DATE",
        date_format="YYYY-MM-DD",
        **kwargs,
    ):
        self.min_date = min_date
        self.max_date = max_date
        self.date_format = date_format
        if isinstance(min_date, str):
            min_date = datetime.strptime(min_date, date_format).date()
        if isinstance(max_date, str):
            max_date = datetime.strptime(max_date, date_format).date()
        super(DateRangeFilter, self).__init__(
            min=Value(">=", min_date),
            max=Value("<=", max_date),
            column_name=column_name,
        )

filter(table)

Filters the given table according to the rules of the Filter.

Parameters:

Name Type Description Default
table PhenexTable

The table to be filtered.

required

Returns:

Name Type Description
PhenexTable PhenexTable

The filtered table. The returned table has the exact same schema as the input table but has rows removed.

Source code in phenex/filters/filter.py
def filter(self, table: PhenexTable) -> PhenexTable:
    """
    Filters the given table according to the rules of the Filter.

    Args:
        table (PhenexTable): The table to be filtered.

    Returns:
        PhenexTable: The filtered table. The returned table has the exact same schema as the input table but has rows removed.
    """
    input_columns = table.columns
    filtered_table = self._filter(table)
    if not set(input_columns) <= set(filtered_table.columns):
        raise ValueError(f"Filter must not remove columns.")

    filtered_table = filtered_table.select(input_columns)
    if isinstance(table, PhenexTable):
        return type(table)(filtered_table)
    else:
        return filtered_table