Filter
Data filtering functionality for Quick Metric.
Provides data filtering capabilities for pandas DataFrames using dictionary-based filter specifications with logical operations.
Functions:
| Name | Description |
|---|---|
evaluate_condition : Evaluate a single filter condition |
|
recursive_filter : Handle complex nested filter conditions |
|
apply_filter : Main entry point for applying filters to DataFrames |
|
Examples:
Simple equality filter:
import pandas as pd
from quick_metric._filter import apply_filter
data = pd.DataFrame({
'category': ['A', 'B', 'A', 'C'],
'value': [10, 20, 30, 40]
})
filter_spec = {'category': 'A'}
filtered_data = apply_filter(data, filter_spec)
print(len(filtered_data)) # 2
Complex nested filter:
filter_spec = {
'and': {
'category': ['A', 'B'],
'value': {'greater than': 15}
}
}
filtered_data = apply_filter(data, filter_spec)
print(len(filtered_data)) # 2
Negation filter:
filter_spec = {
'not': {'category': 'C'}
}
filtered_data = apply_filter(data, filter_spec)
print(len(filtered_data)) # 3
See Also
interpret_instructions : Module that uses filtering before applying methods apply_methods : Module that processes filtered data
evaluate_condition(data_df, column, value)
Evaluate a condition based on the provided column and value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_df
|
DataFrame
|
The DataFrame to be filtered. |
required |
column
|
str
|
The column name to be filtered. |
required |
value
|
Union[dict, Any]
|
The value to be compared against the column. |
required |
Returns:
| Type | Description |
|---|---|
Series
|
Boolean filter mask indicating whether the condition is met for each row in the DataFrame. |
Notes
This function supports the following comparison operators: - less than - less than equal - greater than - greater than equal - is - not - in - not in
Source code in quick_metric/_filter.py
recursive_filter(data_df, filters)
Recursively applies filters to a DataFrame and returns a boolean mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_df
|
DataFrame
|
The DataFrame to filter. |
required |
filters
|
dict
|
A dictionary specifying the filters to apply. The dictionary can contain the keys "and", "or", and "not" to combine conditions recursively. |
required |
Returns:
| Type | Description |
|---|---|
Series
|
A boolean filter mask indicating which rows of the DataFrame match the filters. |
Notes
The filters dictionary can have the following structure: - {"and": {condition1, condition2, ...}}: All conditions must be met. - {"or": {condition1, condition2, ...}}: At least one condition must be met. - {"not": {condition}}: The condition must not be met. - {column: value}: A single condition to apply to a column.
Each condition is a key-value pair where the key is the column name and the value is the condition to apply to that column.
Source code in quick_metric/_filter.py
apply_filter(data_df, filters)
Apply filters to the DataFrame based on the provided dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_df
|
DataFrame
|
The DataFrame to be filtered. |
required |
filters
|
dict
|
Dictionary containing the filter conditions. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Filtered DataFrame. |