-
-
Notifications
You must be signed in to change notification settings - Fork 826
Description
What is your suggestion?
The goal is simply to provide equivalent predicate parsing functionality, that is found in alt.when().
For example, this would be easy to port over:
verbose_composition = (
(alt.datum.Name == "Name_1")
& (alt.datum.Color == "Green")
& (alt.datum.Age == 25)
& (alt.datum.StartDate == "2000-10-01")
)
when_verbose = alt.when(verbose_composition)
when_concise = alt.when(Name="Name_1", Color="Green", Age=25, StartDate="2000-10-01")I was reading through transform/filter and looking at the implementation:
.transform_filter() implementation
altair/altair/vegalite/v5/api.py
Lines 2917 to 2924 in 5a6f70d
| 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)) |
AFAIK, the signature can be changed without impacting any existing code:
altair/altair/vegalite/v5/api.py
Lines 2888 to 2898 in 5a6f70d
| def transform_filter( | |
| self, | |
| filter: str | |
| | Expr | |
| | Expression | |
| | Predicate | |
| | Parameter | |
| | PredicateComposition | |
| | dict[str, Predicate | str | list | bool], | |
| **kwargs: Any, | |
| ) -> Self: |
- There are no checks on
filterbeing in**kwargs - The only "valid" keyword is
emptycondition()has already been safely updated in this way
I definitely want to do some more testing, but I can't see much that would be blocking closing the gap between the two:
altair/altair/vegalite/v5/api.py
Lines 1214 to 1219 in 5a6f70d
| def when( | |
| predicate: Optional[_PredicateType] = Undefined, | |
| *more_predicates: _ComposablePredicateType, | |
| empty: Optional[bool] = Undefined, | |
| **constraints: _FieldEqualType, | |
| ) -> When: |
Side note
#695 is referenced on that page, but I think it was resolved a while ago?
Also there are references to selection() which was deprecated in 5.0.0
Have you considered any alternative solutions?
- Leave
.transform_filter()unchanged - Accept
alt.When(class) asfilter- Would work by unwrapping
When._condition - I'd prefer to add the expressiveness, without annotating
.transform_filterwithWhen- Seems less intuitive to me
- Would work by unwrapping