Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support Chart.transform_filter(*predicates, **constraints) #3664

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5bdb33d
chore(typing): Add temporary alias for `filter`
dangotbanned Oct 30, 2024
2efd5f8
refactor: Make `empty` a regular keyword arg
dangotbanned Oct 30, 2024
d2868f8
refactor: Remove `**kwargs`
dangotbanned Oct 30, 2024
841e887
docs: Add note on `Predicate`
dangotbanned Oct 30, 2024
63de259
feat(DRAFT): Adds `_transform_filter_impl`
dangotbanned Oct 30, 2024
40146f7
Merge remote-tracking branch 'upstream/main' into transform-filter-pr…
dangotbanned Oct 30, 2024
c22ba58
feat: Adds `transform_filter` implementation
dangotbanned Oct 30, 2024
fc672bc
fix(DRAFT): Add temp ignore for `line_chart_with_cumsum_faceted`
dangotbanned Oct 30, 2024
8554a46
feat(typing): Widen `_FieldEqualType` to include `IntoExpression`
dangotbanned Oct 30, 2024
ff9d33f
fix: Try replacing `Undefined` first
dangotbanned Oct 31, 2024
b497039
test: Add `(*predicates, **constraints)` syntax tests
dangotbanned Oct 31, 2024
54d0cbc
Merge branch 'main' into transform-filter-predicates
dangotbanned Nov 2, 2024
a375ab5
Merge remote-tracking branch 'upstream/main' into transform-filter-pr…
dangotbanned Nov 3, 2024
7f6c188
docs: Use `*predicates` in "Faceted Line Chart with Cumulative Sum"
dangotbanned Nov 3, 2024
0d4ff86
Merge remote-tracking branch 'upstream/main' into transform-filter-pr…
dangotbanned Nov 4, 2024
be63d4e
refactor: Remove `_OrigFilterType`
dangotbanned Nov 4, 2024
7a0cc42
docs: Update `.transform_filter()` docstring
dangotbanned Nov 4, 2024
08a4207
docs: Minor corrections in examples
dangotbanned Nov 4, 2024
5fd4f5a
refactor(typing): Cast deprecated `filter` in one location
dangotbanned Nov 4, 2024
d640933
refactor: Remove `pred` assignment
dangotbanned Nov 4, 2024
2d57d6e
docs(typing): Update `_FieldEqualType`
dangotbanned Nov 4, 2024
01571dd
Merge branch 'main' into transform-filter-predicates
dangotbanned Nov 4, 2024
10f1d1d
Merge branch 'main' into transform-filter-predicates
dangotbanned Nov 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 27 additions & 16 deletions altair/vegalite/v5/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,18 @@ def check_fields_and_encodings(parameter: Parameter, field_name: str) -> bool:
]
"""Permitted types for `predicate`."""

# FIXME: Remove before merging
_OrigFilterType: TypeAlias = "str | Expr | Expression | Predicate | Parameter | PredicateComposition | dict[str, Predicate | str | list | bool]"
"""**Temporary** alias for ``transform_filter``'s original annotation.

Notes
-----
- Quite similar to ``_PredicateType``
- Probably some redundant typing, that can be reduced
- ``Predicate`` derives ``PredicateComposition``
- Includes all ``(Logical|Field|Parameter)...Predicate``
"""

_ComposablePredicateType: TypeAlias = Union[
_expr_core.OperatorMixin, SelectionPredicateComposition
]
Expand Down Expand Up @@ -2949,14 +2961,10 @@ def transform_extent(
# # E.g. {'not': alt.FieldRangePredicate(field='year', range=[1950, 1960])}
def transform_filter(
self,
filter: str
| Expr
| Expression
| Predicate
| Parameter
| PredicateComposition
| dict[str, Predicate | str | list | bool],
**kwargs: Any,
predicate: Optional[_PredicateType] = Undefined,
*more_predicates: _ComposablePredicateType,
empty: Optional[bool] = Undefined,
**constraints: _FieldEqualType,
) -> Self:
"""
Add a :class:`FilterTransform` to the schema.
Expand All @@ -2976,14 +2984,17 @@ def transform_filter(
self : Chart object
returns chart to allow for chaining
"""
if isinstance(filter, Parameter):
new_filter: dict[str, Any] = {"param": filter.name}
if "empty" in kwargs:
new_filter["empty"] = kwargs.pop("empty")
elif isinstance(filter.empty, bool):
new_filter["empty"] = filter.empty
filter = new_filter
return self._add_transform(core.FilterTransform(filter=filter, **kwargs))
if depr_filter := constraints.pop("filter", None):
utils.deprecated_warn(
"Passing `filter` as a keyword is ambiguous.\n\n"
"Use a positional argument for `<5.5.0` behavior.\n"
"Or, `alt.datum['filter'] == ...` if referring to a column named 'filter'.",
version="5.5.0",
)
more_predicates = *more_predicates, t.cast(Any, depr_filter)
cond = _parse_when(predicate, *more_predicates, empty=empty, **constraints)
pred = cond.get("test", cond)
return self._add_transform(core.FilterTransform(filter=pred))

def transform_flatten(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
columns_sorted = ['Drought', 'Epidemic', 'Earthquake', 'Flood']

alt.Chart(source).transform_filter(
{'and': [
{'and': [ # type: ignore[arg-type]
dangotbanned marked this conversation as resolved.
Show resolved Hide resolved
alt.FieldOneOfPredicate(field='Entity', oneOf=columns_sorted), # Filter data to show only disasters in columns_sorted
alt.FieldRangePredicate(field='Year', range=[1900, 2000]) # Filter data to show only 20th century
]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
columns_sorted = ['Drought', 'Epidemic', 'Earthquake', 'Flood']

alt.Chart(source).transform_filter(
{'and': [
{'and': [ # type: ignore[arg-type]
alt.FieldOneOfPredicate(field='Entity', oneOf=columns_sorted), # Filter data to show only disasters in columns_sorted
alt.FieldRangePredicate(field='Year', range=[1900, 2000]) # Filter data to show only 20th century
]}
Expand Down