Skip to content

Update Chart.transform_filter() to mirror alt.when() #3657

@dangotbanned

Description

@dangotbanned

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

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:

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 filter being in **kwargs
  • The only "valid" keyword is empty
    • condition() 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:

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) as filter
    • Would work by unwrapping When._condition
    • I'd prefer to add the expressiveness, without annotating .transform_filter with When
      • Seems less intuitive to me

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions